awk实例-计算加总并算出平均值
awk实例-计算加总并算出平均值
20121108
常常会遇到一个档案内容都是数字, 一个数字一行, 要加总, 然后要算出平均值, 要怎么做呢?
档案 count.txt
vim count.txt
1
2
3
4
5
计算加总
$ cat count.txt | awk '{sum+=$1} END {print "Sum = ", sum}'
Sum = 15.9
计算平均值
$ cat count.txt | awk '{sum+=$1} END {print "Average = ", sum/NR}'
Average = 3.18