awk 列求和


awk 'BEGIN{sum=0}{sum+=$1}END{print sum}' data.txt

有a、b、c三行三列,计算出第二列和第三列的总和,并在第四行输出

[root@test ~]# cat data.txt
a    1      10
b   26     379
c    2    1605

[root@test ~]# awk '{printf "%10s%5s%8s\n",$1,$2,$3;x+=$2;y+=$3}END{printf "%10s%5s%8s\n","Total:",x,y}' data.txt
     a    1      10
     b   26     379
     c    2    1605
Total:   29    1994
分享:

评论