对日志文件的IP出现的次数进行统计,并显示次数最多的前5名

Nginx日志格式如下所示:

  1. log_format main '$remote_addr - $remote_user [$time_local] '
  2. 'fwf[$http_x_forwarded_for] '
  3. '$http_host $request '
  4. '$status $upstream_response_time $request_time '
  5. '$request_length '
  6. '$body_bytes_sent $http_referer '
  7. '$http_user_agent $server_addr $http_x_real_ip';

日志内容示例如下:

  1. 47.112.193.218 - - [21/Jan/2021:00:51:55 +0800] fwf[-] txw-api.dhclgl.com GET / HTTP/1.1 200 0.002 0.003 149 31 - python-requests/2.20.1 192.168.1.15 -
  2. 47.112.193.218 - - [21/Jan/2021:03:35:57 +0800] fwf[-] txw-api-pro.dhclgl.com GET / HTTP/1.1 200 0.002 0.003 153 32 - python-requests/2.20.1 192.168.1.15 -
  3. 47.112.193.218 - - [21/Jan/2021:04:55:16 +0800] fwf[-] txw-api.dhclgl.com GET / HTTP/1.1 200 0.004 0.003 149 31 - python-requests/2.20.1 192.168.1.15 -
  4. 47.112.193.218 - - [21/Jan/2021:07:37:25 +0800] fwf[-] txw-api-pro.dhclgl.com GET / HTTP/1.1 200 0.003 0.003 153 32 - python-requests/2.20.1 192.168.1.15 -
  5. 47.112.193.218 - - [21/Jan/2021:08:51:27 +0800] fwf[-] txw-api.dhclgl.com GET / HTTP/1.1 200 0.002 0.002 149 31 - python-requests/2.20.1 192.168.1.15 -
  6. 47.112.193.218 - - [21/Jan/2021:11:36:00 +0800] fwf[-] txw-api-pro.dhclgl.com GET / HTTP/1.1 200 0.003 0.003 153 32 - python-requests/2.20.1 192.168.1.15 -

awk处理如下:

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