HJM

导航

shell练习题6

需求如下:

有日志access.log,部分内容如下:

127.0.0.1 - - [03/Jul/2018:00:00:01 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0"
127.0.0.1 - - [03/Jul/2018:00:01:48 +0800] "GET /1.php HTTP/1.1" 200 19 "-" "curl/7.29.0"
192.168.1.9 - - [04/Jul/2018:23:43:19 +0800] "GET / HTTP/1.1" 200 13 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
192.168.1.9 - - [04/Jul/2018:23:43:19 +0800] "GET /favicon.ico HTTP/1.1" 404 571 "http://192.168.1.5/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
192.168.1.9 - - [04/Jul/2018:23:45:30 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
192.168.1.9 - - [04/Jul/2018:23:45:32 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
192.168.1.9 - - [04/Jul/2018:23:45:33 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
192.168.1.9 - - [04/Jul/2018:23:45:34 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
192.168.1.9 - - [04/Jul/2018:23:45:35 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"

编译出每个IP的访问量有多少?

参考解答如下

  • 方法1
 cut -d ' ' -f 1 access.log | sort -n | uniq -c | sort -nr
  • 方法2
# sort -n按数字方式排序
# uniq -c前一定要先排序
# sort  -nr 倒序
awk '{print $1}' access.log | sort -n | uniq -c | sort -nr
  • 方法3
awk '{ip[$1]++}END{for(i in ip){print i,ip[i]}}' access.log

posted on 2018-10-25 22:07  kennminn  阅读(126)  评论(0编辑  收藏  举报