使用powershell的Excel平均总和

时间:2014-07-01 08:52:22

标签: excel powershell

我有一个包含3列的CSV文件:

Value0  Value1  Value2  Value3
Test1   2.43    0         2
Test2   1.5 0   1         5
Test3   18.12   0.1       1
Test4   7.99    0         2
Test5   3.84    0.1       2

我想要做的是找到值1,2和3的平均值,并使用powershell将它们写在csv文件中。

任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:1)

缺点是这样:

# Import the CSV file into an object
$data = import-csv filename.csv

# Add a new column to hold the averages
$data = $data|Select *,Average

# Iterate through the object, get the average from each row and assign to the added column
ForEach ($row in $data) {
$row.Average = $data.Value1, $data.Value2, $data.Value3|measure -Average|Select -expand Average
}

# Export back out to a CSV
$data|Export-csv outputfilename.csv -NoType