在我的.csv文件中,我有四列数字,定义如下:
aIN = $item1.IN
aOUT = $item1.OUT
bIN = $item2.IN
bOUT = $item2.OUT
数字本身是整数和小数的混合。我正在尝试使用以下方法查找total-IN列和total-OUT列:
total-IN = aIN + bIN
total-OUT = aOUT + bOUT
假设我有...
aIN aOUT bIN bOUT
0.1 0.2 0.3 0.4
1 2 3 4
0.5 0.6 0.7 0.8
5 6 7 8
我想要的是...
aIN aOUT bIN bOUT total-IN total-OUT
0.1 0.2 0.3 0.4 0.4 0.6
1 2 3 4 4 6
0.5 0.6 0.7 0.8 1.2 1.4
5 6 7 8 12 14
我的方法不起作用。预先感谢您的帮助!
答案 0 :(得分:4)
使用
Select-Object
追加总列$CsvData = Import-Csv '.\testfile.csv' | Select-Object *,'total-IN','total-OUT'
ForEach ($Row in $CsvData) {
$Row.'total-IN' = [double]$Row.aIN + $Row.bIN
$Row.'total-OUT' = [double]$Row.aOUT + $Row.bOUT
}
$CsvData | Format-Table -AutoSize
$CsvData | Export-Csv .\your.csv -NoTypeInformation
您也可以使用calculated property
$CsvData = @"
aIN,aOUT,bIN,bOUT
0.1,0.2,0.3,0.4
1,2,3,4
0.5,0.6,0.7,0.8
5,6,7,8
"@ | ConvertFrom-Csv | Select-Object *,
@{n='total-IN';e={[double]$_.aIN + $_.bIN}},
@{n='total-OUT';e={[double]$_.aOUT + $_.bOUT}}
$CsvData | Format-Table -AutoSize
$CsvData | Export-Csv .\your.csv -NoTypeInformation