(1)awk求和
cat data |awk '{sum+=$1} END {print "Sum = ", sum}'
(2)平均值
cat data|awk '{sum+=$1} END {print "Average = ", sum/NR}'
(3)求最大值
cat data|awk 'BEGIN {max = 0} {if ($1>max) max=$1 fi} END {print "Max=", max}'
(4)求最小值(min的初始值设置一个超大数即可)
cat data | awk 'BEGIN {min = 1999999} {if ($1<min) min=$1 fi} END {print "Min=", min}'
(5)统计文件test.txt中第2列不同值出现的次数
awk '{sum[$2]+=1}END{for(i in sum)print i" "sum[i]}' test.txt
(6)如只统计文件test.txt中第2列"00"或"01"出现的次数,命令可写为
awk '{if($2=="00") ++sum1;if($7=="01") ++sum2}END{print "00"" "sum1" ""01"" "sum2}' test.txt
(7)统计文本各个字段出现次数
awk '{ for (i=1;i<=NF;i++){a[$i]++} } END{for (b in a) print b": "a[b]}' test.txt