统计Apache或nginx日志里访问次数最多的IP

Nginx

awk '{print $1}’/usr/local/nginx/logs/domain.access.log|sort |uniq –c|head -10

cat /usr/local/nginx/logs/domain.access.log|awk '{print $1}’| sort |uniq –c|head -n 10

Apache

awk '{print $1}' /usr/local/apache2/logs/access_log|uniq -c |sort -rn| head -n 10
cut -d- -f 1 /usr/local/apache2/logs/access_log |uniq -c | sort -rn | head -10
注:以上3种方法统计前十名的IP,head –n选项可以省略。cat占用很多内存,不推荐使用,awk省资源快捷
其他一些分析日志的shell用法:
1、查看当天有多少个IP访问:
awk '{print $1}' log_file|sort|uniq|wc -l
2、查看某一个页面被访问的次数;
grep "/index.php" log_file | wc -l
3、查看每一个IP访问了多少个页面:
awk '{++S[$1]} END {for (a in S) print a,S[a]}' log_file
4、将每个IP访问的页面数进行从小到大排序:
awk '{++S[$1]} END {for (a in S) print S[a],a}' log_file | sort -n
5、查看某一个IP访问了哪些页面:
grep ^121.141.161.211 log_file| awk '{print $1,$7}'
 
6、去掉搜索引擎统计当天的页面:
awk '{print $12,$1}' log_file | grep ^\"Mozilla | awk '{print $2}' |sort | uniq | wc -l
7、查看2009年6月21日14时这一个小时内有多少IP访问:
 awk '{print $4,$1}' log_file | grep 21/Jun/2009:14 | awk '{print $2}'| sort | uniq | wc -l