简体   繁体   English

PowerShell:将CSV的每一行导出到单个文件

[英]PowerShell: Export each line of CSV to individual file

I have a tab-delimited CSV which needs to be broken apart into separate CSVs for each row, with the filename being [FirstName] [LastName].csv. 我有一个制表符分隔的CSV,需要将每一行拆分为单独的CSV,文件名为[FirstName] [LastName] .csv。

Additionally, I need to manipulate some data, specifically remove the dashes from the SSN. 另外,我需要处理一些数据,特别是从SSN中删除破折号。

Sample Content 样本内容

FirstName  LastName   SSN
=========  =========  ======
Albert     Abernathy  111-11-1111
Billy      Barty      222-22-2222
Chris      Canton     333-33-3333

Desired output (Filename | CSV Content) 所需的输出 (文件名| CSV内容)

Albert Abernathy.csv | Albert     Abernathy  111111111
Billy Barty.csv      | Billy      Barty      222222222
Chris Canton.csv     | Chris      Canton     333333333

Here's what I have so far. 到目前为止,这就是我所拥有的。

$csvFile = "C:\Test\List.csv"
$folderPath = Split-Path -Parent $csvFile
$csv = Import-Csv -Path $csvFile -Delimiter "`t" 
| Where-Object {$_.LastName -ne ""} ##Prevent importing blank lines
| Foreach-Object {$_.SSN -Replace "-","" } ##Remove dashes from SSN
foreach ($line in $csv)
{   $saveName = $line.FirstName + " " + $line.LastName + ".csv"
    Export-Csv -Delimiter "`t" -Path $folderPath\$saveName
}

So far, all of this runs, but it just pauses after the final clause and nothing happens. 到目前为止,所有这些都运行了,但是它只是在最后一个子句之后暂停,什么也没有发生。 As I cobbled this together from Google searches, I'm certain my syntax needs work, so I'd be grateful if someone could point me in the right direction 当我从Google搜索中搜集到这些内容时,我确定我的语法需要工作,因此,如果有人能指出正确的方向,我将不胜感激

--Update-- -更新-

Credit to alroc for pointing me in the right direction, and mjolinor for providing an invaluable REPLACE syntax I could not find anywhere else. 感谢alroc为我指明了正确的方向,而mjolinor则提供了我在其他任何地方都找不到的宝贵的REPLACE语法。 Here's my final script. 这是我的最终脚本。

$csvFile = "C:\Test\List.csv"
$folderPath = Split-Path -Parent $csvFile

### filter out blank records and delete dashes from SSN ###
(Get-Content $csvFile | 
    Select-Object |
    Where-Object {$_.LastName -ne ""}) | #Rows with non-blank last name
    Foreach-Object {$_ -Replace '(\d{3})-(\d{2})-(\d{4})','$1$2$3'} |
    Set-Content $csvFile

### import csv data and export file for each row ###
$csv = Import-Csv -Path $csvFile -Delimiter "`t"
foreach ($row in $csv) {
    $outPath = Join-Path -Path $folderPath -ChildPath $($row.FirstName + " " + $row.LastName + ".csv");
    $row | Select-Object | 
        ConvertTo-Csv -NoTypeInformation -Delimiter "`t" | 
        Foreach-Object {$_.Replace('"','')} | #Delete quotes around each "cell"
        Out-File $outPath
    (Get-Content $outPath | 
        Select-Object -Skip 1) | #Delete header
        Set-Content $outPath
}

I'm not clear on what you mean by "it just pauses after the final clause and nothing happens" but it may have something to do with you piping import-csv to the rest of your process and attempting to assign the result to $csv . 我不清楚您的意思是“它只是在最后一个子句之后暂停而什么也没有发生”,但它可能与您import-csv传递到其余过程并将尝试将结果分配给$csv This works for me: 这对我有用:

$csvFile = "C:\Test\List.csv"
$folderPath = Split-Path -Parent $csvFile
$csv = Import-Csv -Path $csvFile -Delimiter "`t" 
foreach ($record in $csv) {
    $OutFileName = join-path -Path $folderPath -ChildPath $($record.FirstName + " " + $record.LastName + ".csv");
    $record|select-object FirstName,LastName,@{name="SSN";expression={$_.SSN -replace "-",""}}|export-csv -NoTypeInformation -Path $OutFileName -Delimiter "`t";
}

Or if you want to do it without the intermediate variables, keeping it more pipeline-y: 或者,如果您想在没有中间变量的情况下执行此操作,请使其更加流水线化:

$csvFile = "C:\Test\List.csv"
$folderPath = Split-Path -Parent $csvFile
Import-Csv -Path $csvFile -Delimiter "`t" | foreach {
    $OutFileName = join-path -Path $folderPath -ChildPath $($_.FirstName + " " + $_.LastName + "2.csv");
    $_|select-object FirstName,LastName,@{name="SSN";expression={$_.SSN -replace "-",""}}|export-csv -NoTypeInformation -Path $OutFileName -Delimiter "`t";
}

FWIW - Not tested. FWIW-未测试。 Kept as much of the original script as possible. 保留尽可能多的原始脚本。

$csvFile = "C:\Test\List.csv"
$folderPath = Split-Path -Parent $csvFile.fullname

Import-Csv -Path $csvFile -Delimiter "`t" |
 Where-Object {$_.LastName -ne ""} |  ##Prevent importing blank lines
 Foreach-Object {
  $_.SSN = $_.SSN -Replace "-",""  ##Remove dashes from SSN
  $saveName = $_.FirstName + " " + $_.LastName + ".csv" 
  Export-Csv $_ -Delimiter "`t" -Path $folderPath\$saveName
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM