用新行排序csv

时间:2017-10-17 06:10:46

标签: powershell csv sorting format

我收到了用户和工作站的报告。现在,如果用户有多个工作站,我希望每个工作站都有一个单独的行。始终使用Name,Surename,Workstation。最后,它应该作为report-new.csv导出。

CSV:

"Name","Surname","Workstation"
"John","Doe","PC001,PC005,PC100"
"Fonzarelli","Arthur","PC234,PC324"
"Tribbiani","Joey","PC999"

脚本:

$report = Import-Csv "C:\Scripts\report.csv"

Foreach ($wst in $report) {

    If ($wst.Workstation -match ",") {

        New Line foreach Workstation? Export-Csv "C:\Scripts\report-new.csv" -NoTypeInformation
    }
}

2 个答案:

答案 0 :(得分:1)

$Report | ForEach {ForEach ($ws in ($_.workstation -split ",")) {
    $_ | Select Name, SurName, @{Name = "Workstation"; Expression = {$ws}}}
} | Export-Csv "C:\Scripts\report-new.csv" -NoTypeInformation

结果:

Name       Surname Workstation
----       ------- -----------
John       Doe     PC001
John       Doe     PC005
John       Doe     PC100
Fonzarelli Arthur  PC234
Fonzarelli Arthur  PC324
Tribbiani  Joey    PC999

答案 1 :(得分:0)

您可以用逗号拆分 Workstation属性,并使用换行符加入。例如:

$report = Import-Csv "C:\Scripts\report.csv"

Foreach ($wst in $report) {

    ($wst.Workstation -split ',') -join [System.Environment]::NewLine
}