我正在编写一个bash脚本来迭代具有给定值的文件行。
我用来列出可能值的命令是:
cat file.csv | cut -d';' -f2 | sort | uniq | head
当我在这样的循环中使用它时它会停止工作:
for i in $( cat file.csv | cut -d';' -f2 | sort | uniq | head )
do
//do something else with these lines
done
如何在for循环中使用管道命令?
答案 0 :(得分:1)
您可以使用此awk命令获取第2列的每个唯一值的第3列的总和:
awk -F ';' '{sums[$2]+=$3} END{for (i in sums) print i ":", sums[i]}' file.csv
输入数据:
asd;foo;0
asd;foo;2
asd;bar;1
asd;foo;4
<强>输出:强>
foo: 6
bar: 1