Apache日志:按服务字节计算前10个URL

时间:2011-09-27 13:38:59

标签: linux bash awk

我有一个Apache日志格式文件。示例字符串:

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中做到这一点?

2 个答案:

答案 0 :(得分:5)

这对你有用吗?

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

答案 1 :(得分: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