【问题标题】:PowerShell: Export each line of CSV to individual filePowerShell:将每行 CSV 导出到单个文件
【发布时间】:2013-10-24 18:56:41
【问题描述】:

我有一个制表符分隔的 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
}

【问题讨论】:

    标签: powershell csv


    【解决方案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";
    }
    

    【讨论】:

    • 这两个都像宣传的那样工作。有什么方法可以避免在输出中指定每一列?实际场景大约有 60 列。
    • 使用 ConvertTo-CSV、Out-File 和中间的 Replace,我能够输出不带引号的文件。 social.technet.microsoft.com/Forums/windowsserver/en-US/…
    • ($record | select-object | ConvertTo-Csv -NoTypeInformation -Delimiter "`t")[1].replace('"','') -replace '(\d{3} )-(\d{2})-(\d{4})','$1$2$3'| 输出文件 $OutFileName
    • 您的 REPLACE 子句正是我一直在寻找的!
    【解决方案2】:

    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
      }
    

    【讨论】:

    • 对于这个,我得到以下 PS 提示:cmdlet Export-Csv at command pipeline position 1 为以下参数提供值:InputObject:
    • 添加了 $_ 以使用当前管道对象作为导出的 InputObject。
    猜你喜欢
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 2018-02-21
    • 2017-06-28
    • 2015-07-06
    • 2016-04-20
    • 2016-03-29
    • 2014-06-01
    相关资源
    最近更新 更多