例如有test.txt文件
1 aaa 1000
2 bbb 1000
3 aaa 1000
4 aaa 1000
5 ccc 1000
6 bbb 1000
要求产生新文件格式为
1 aaa 3000
2 bbb 2000
5 ccc 1000
请各位高手指点,谢谢!
解决方法如下:
(1):
awk '{if(a[$2]==0)a[$2]=$1;b[$2]+=$3}END{for(i in b){print a[i],i,b[i]}}' test.txt |sort
(2):
awk '!a[$2]++{b[++n]=$1" "$2}{c[$2]+=$3}END{for (i=1;i<=n;i++) {split(b[i],d," ");print b[i],c[d[2]]}}' test.txt
阅读(961) | 评论(0) | 转发(0) |