2011-09-27 21 views
0

我有一个Apache日志格式文件。示例字符串:Apache日志:计数排名前10的URL按字节服务

fj5020.inktomisearch.com - - [01/Oct/2006:06:35:59 -0700] "GET /example/When/200x/2005/04/27/A380 HTTP/1.0" 200 4776 "-" "Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)" 

其中4776页面大小以字节为单位。我想通过提供的流量输出前10个网址。我坚持总结每个独特页面的所有大小的问题(页面的大小也可以是可变的)。任何想法如何在Bash或/和AWK中做到这一点?

回答

5

这个工作适合您吗?

awk '{a[$7]+=$10}END{for(x in a)print x, a[x]}' yourLogfile|sort -r -n -k2|head -n10 
+0

谢谢,这是我需要的。非常感激。 – bvk256

0

很多方法可以做到这一点。这是一个。

total=0 
last_site= 
while read site size ; do 
    if [ "$site" != "$last_site" ] ; then 
     [ ! -z "$last_site" ] && printf '%s %d\n' "$last_site" $total 
     total=0 
     last_site="$site" 
    fi 
    let total+=$size 
done < <(awk '{print $1, $10}' log | sort) 

printf '%s %d\n' "$last_site" $total