awk实现求和、平均、最大值和最小值的计算操作
比如有一个数据文件,只有一列(在之前可以通过各种手段过滤出只有数字这一列),比如操作的响应时间
1# cat data
2490898
31189235
420212
51494270
6146515
729369
823563
9563027
1022976
11127809
1216813
13551646
1418858
1518977
1、求和
1cat data|awk '{sum+=$1} END {print "Sum = ", sum}'
2、求平均
1cat data|awk '{sum+=$1} END {print "Average = ", sum/NR}'
3、求最大值
1cat data|awk 'BEGIN {max = 0} {if ($1>max) max=$1 fi} END {print "Max=", max}'
4、求最小值(min的初始值设置一个超大数即可)
1awk 'BEGIN {min = 1999999} {if ($1<min) min=$1 fi} END {print "Min=", min}'
5、求访问次数的Top 10 Resource,可以根据此进行优化
1cat output/logs/cookie_logs/`date +%u`/cookie_log|grep -v '172.16'|grep -v '127.0.0.1' |awk -F' ' '{ if(index($1,"219.141.246")!=0) print $2; else print $1 } '|sort|uniq -c|sort -n |tail -n 10
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/awk-sum-max-min/4026.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.