首页 > 代码库 > awk多个数组的使用

awk多个数组的使用

#!/bin/bashawk BEGIN{printf "%-60s %-10s %-10s %-10s\n","url","count","time","avg_time"}$NF ~ /^[0-9]/{c1[$11]+=$NFc2[$11]++}END{for(b in c2) {    for(a in c1)         if(a == b)        printf "%-60s %-10s %-10s %-10s\n", a,c2[b],c1[a],c1[a]/c2[b]        continue}} log.txt~           

注释:

c1统计每个url对应的总响应时间

c2统计每个url对应的总请求个数

每次循环,外围c2的值是不变的,内围c1处于变化中,当c1的url和c2相等时,则输出对应数据,同时跳出此次内循环,开启下一次新循环

获取每个url的平均响应时间

日志格式如下:

2014-06-20 16:47:43  [ resin-port-18057-51:333230 ] - [ INFO ]  [url] http://abc.com/list 254
2014-06-20 16:47:43  [ resin-port-18057-51:333230 ] - [ INFO ]  [url] http://abc.com/list 254 

2014-06-20 16:47:43  [ resin-port-18057-51:333230 ] - [ INFO ]  [url] http://abc.com/list 254 

2014-06-20 16:47:43  [ resin-port-18057-51:333230 ] - [ INFO ]  [url] http://abc.com/list 254

执行效果如下:

url                                                               count      time       avg_time 
http://a.9tong.com/plat/active.do                           185         31            0.167568 
http://a.9tong.com/product/updatePic.do                 80          16465      205.812  
http://a.9tong.com/product/shopList.do                   1133       51663      45.5984