awk是个好工具

今天遇到一个需求,要统计系统哪些功能耗费的流量最大并后续优化,去acc log看了以下第5列是http请求的字节数,第10列是http请求的url,写了一个类似于sql的group by、sum、avg命令,运行OK,结果一目了然


awk '{bytes[$10]+=$5;count[$10]+=1}END{for(i in bytes) printf "%s %d %10.2f %10.2f \n",i,count[i],bytes[i]/1024/1024,bytes[i]/count[i]}' *.acc | sort -k 3 -rg | awk 'BEGIN{printf "请求url 请求总数 总字节数(MB) 平均每次请求字节数\n"}{print $0}' | less


其中数组bytes[]存储http请求总的流量,数组count[]存储http请求总的次数,最后用sort命令根据流量(-k 3 -g)倒序(-r 参数)排序,最后用less命令逐屏展示

awk用法参考: http://www.linux.gov.cn/shell/awk_ibm.htm
当然学习所有linux命令最好的方法就是man <your command>

猜你喜欢

转载自maxeric2007.iteye.com/blog/1199551
今日推荐