1.awk 求和
cat ckl.txt |awk '{sum+=$1} END {print "Sum = ", sum}'
2.awk 求平均
cat ckl.txt|awk '{sum+=$1} END {print "Average = ", sum/NR}'
3.awk 求最大
cat ckl.txt|awk 'BEGIN {max = 0} {if ($1>max) max=$1 fi} END {print "Max=", max}'
4.awk 求最小值(min的初始值可以设置一个比较大的值)
awk 'BEGIN {min = 100000} {if ($1<min) min=$1 fi} END {print "Min=", min}'
5.awk 统计TCP状态
netstat -ant | awk '/^tcp/{STA[$NF]++}END{for ( k in STA) {print k,STA[k]}}'
6.awk 统计IP访问量
cat access.log | awk '{ACC[$1]++}END{for (c in ACC) {print c,ACC[c]}}'
7.awk 去重复
# cat ckl.txt mkb:8899 ccbb:88ds mkb:9088 jdsd:762 nnc:883 hdxa:77 jdsd:90111
可以看出mkb和jdsd 有重复
去重:
#awk -F':' '!a[$1]++' ckl.txt mkb:8899 ccbb:88ds jdsd:762 nnc:883 hdxa:77
转载于:https://blog.51cto.com/ckl893/1760881