人人网一个笔试题就是要求用shell分析Apache日志(/usr/local/apache2/logs/access_log),要求列出当天访问次数最多的IP,下面是典型的访问信息的log。用shell列出访问最多的20个ip:
180.168.41.175 - - [23/Aug/2011:14:47:37 -0400] "GET / HTTP/1.0" 200 654
216.35.116.91 - - [23/Aug/2011:14:47:37 -0400] "GET / HTTP/1.0" 200 654
124.90.136.42 - - [23/Aug/2011:14:47:37 -0400] “GET / HTTP/1.1″ 200 44
命令:cut -d- -f 1 /usr/local/apache2/logs/access_log |uniq -c | sort -rn | head -20
原理:
cut
-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter
表示用-分割,然后-f 1
-f, --fields=LIST
select only these fields; also print any line that contains no
delimiter character, unless the -s option is speci
180.168.41.175 - - [23/Aug/2011:14:47:37 -0400] "GET / HTTP/1.0" 200 654
216.35.116.91 - - [23/Aug/2011:14:47:37 -0400] "GET / HTTP/1.0" 200 654
124.90.136.42 - - [23/Aug/2011:14:47:37 -0400] “GET / HTTP/1.1″ 200 44
命令:cut -d- -f 1 /usr/local/apache2/logs/access_log |uniq -c | sort -rn | head -20
原理:
cut
-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter
表示用-分割,然后-f 1
-f, --fields=LIST
select only these fields; also print any line that contains no
delimiter character, unless the -s option is speci