我正在根据BusinessUnit
(它是csv文件中的一列)将csv文件拆分为较小的csv文件。
直到现在我都尝试过:-
$fullpath = "C:\Batch\Output Folder\ErroreportforAllregions.csv"
$path = Split-Path $fullpath -parent
$data = Import-CSV -Path $fullpath -Header BusinessUnit,Date,Time,PolicyNum
foreach ($group in $data | Group BusinessUnit){
$data | Where-Object {$_.species -eq $group.name} |
ConvertTo-Csv -NoTypeInformation |
foreach {$_.Replace('"','')}|
Out-File "$path\$($group.name).csv"
}
但是在这种情况下,行(数据)不在小型csv文件中。请指导我哪里出错了。
我在大型csv文件中的数据:-
BusinessUnit serialno
Eastern Region 1
Ottawa 2
India 3
输出应为
答案 0 :(得分:1)
假设您的大型csv实际上是这样的:
"BusinessUnit","Date","Time","PolicyNum","serialno"
"Eastern Region","2019-02-04","11:09:45","123","1"
"Eastern Region","2019-02-03","10:09:45","123","1"
"Ottawa","2019-02-04","11:09:45","123","1"
"India","2019-02-04","11:09:45","123","1"
然后应该这样做:
$fullpath = "C:\Batch\Output Folder\ErroreportforAllregions.csv"
$path = Split-Path $fullpath -parent
$data = Import-CSV -Path $fullpath
$data | Group-Object -Property BusinessUnit | ForEach-Object {
$file = Join-Path -Path $path -ChildPath ($_.Name + '.csv')
$_.Group | Export-Csv -Path $file -NoTypeInformation
}
使用上方的数据,它将创建三个名为Eastern Region.csv
,Ottawa.csv
和India.csv
的csv文件。
东部地区.csv
"BusinessUnit","Date","Time","PolicyNum","serialno"
"Eastern Region","2019-02-04","11:09:45","123","1"
"Eastern Region","2019-02-03","10:09:45","123","1"
Ottawa.csv
"BusinessUnit","Date","Time","PolicyNum","serialno"
"Ottawa","2019-02-04","11:09:45","123","1"
India.csv
"BusinessUnit","Date","Time","PolicyNum","serialno"
"India","2019-02-04","11:09:45","123","1"
希望这会有所帮助