助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
## **对日志文件的IP出现的次数进行统计,并显示次数最多的前5名** ### **Nginx日志格式如下所示:** ``` log_format main '$remote_addr - $remote_user [$time_local] ' 'fwf[$http_x_forwarded_for] ' '$http_host $request ' '$status $upstream_response_time $request_time ' '$request_length ' '$body_bytes_sent $http_referer ' '$http_user_agent $server_addr $http_x_real_ip'; ``` ### **日志内容示例如下:** ``` 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 - 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 - 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 - 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 - 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 - 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处理如下:** ``` awk '{print $1}' test.log | uniq -c | sort -nr | head -5 ```