【面试题】分析nginx访问日志,找出访问页面数量前十的ip

nginx访问日志access.log的日志格式:

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

第一个字段就是访问的ip。
因此获取访问页面数量前十的ip的方法为:

cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head

思路:每条访问记录占一行,获取第一个字段的ip,排序后去重,再按出现次数降序排序,最后打印前十个。
uniq -c 去重后再行首标记重复次数
sort -nr 逆序排序
head 默认打印前十个

[root@Charramma logs]# cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head
     21 119.90.20.254
      5 119.90.20.25
      4 119.90.21.2
      4 119.90.20.253
      2 119.90.2.254
      2 119.90.22.34
      2 119.90.22.3
      2 119.90.2.2
      2 119.90.20.23
      1 119.9.20.254
原文地址:https://www.cnblogs.com/CharrammaBlog/p/13723041.html