前言
今天我们来讲讲如何用awk进行网站日志分析,得到页面平均耗时排行
文件
[xingxing.dxx@30_28_6_20 ~]$ cat logs 222.83.181.42 - - [09/Oct/2010:04:04:03 +0800] GET /pages/international/tejia.php HTTP/1.1 "200" 15708 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Sicent; WoShiHoney.B; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)" "-" 0.037
按照空格分隔的话,第7个字段 是页面访问地址,最后一个字段[0.037] 是页面执行时间
[xingxing.dxx@30_28_6_20 ~]$ awk '{print $7,$NF}' logs /pages/international/tejia.php 0.037
代码
awk 'BEGIN{ print "Enter log file:"; getline loga; while(getline < loga) { split($7,atmp,"?"); aListNum[atmp[1]]+=1; aListTime[atmp[1]]+=$NF; ilen++; } close(loga); print "\r\ntotal:",ilen,"\r\n======================================\r\n"; for(k in aListNum) { print k,aListNum[k],aListTime[k]/aListNum[k] | "sort -r -n -k3"; } }'
结果
total: 1 ====================================== /pages/international/tejia.php 1 0.037