0

我有一个制表符分隔的 CSV,需要将每一行分成单独的 CSV,文件名为 [FirstName] [LastName].csv。

此外,我需要处理一些数据,特别是从 SSN 中删除破折号。

样本内容

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

所需的输出(文件名 | CSV 内容)

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

这是我到目前为止所拥有的。

$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
}

到目前为止,所有这些都运行了,但它只是在最后一个子句之后暂停,没有任何反应。当我从谷歌搜索中拼凑出来时,我确定我的语法需要改进,所以如果有人能指出我正确的方向,我将不胜感激

- 更新 -

感谢 alroc 为我指明了正确的方向,感谢 mjolinor 提供了我在其他任何地方都找不到的宝贵的 REPLACE 语法。这是我的最终脚本。

$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
}
4

2 回答 2

1

我不清楚您所说的“它只是在最后一个子句之后暂停并且什么都没有发生”是什么意思,但这可能与您管道import-csv到流程的其余部分并尝试将结果分配给$csv. 这对我有用:

$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";
}

或者,如果您想在没有中间变量的情况下执行此操作,请保持更多管道:

$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";
}
于 2013-10-24T19:55:08.740 回答
0

FWIW - 未经测试。尽可能多地保留原始脚本。

$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
  }
于 2013-10-24T19:55:47.623 回答