我有一个制表符分隔的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
}
答案 0 :(得分: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";
}
或者如果你想在没有中间变量的情况下做到这一点,请保持更多的管道-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";
}
答案 1 :(得分: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
}