Linux中根据访问日志统计访问量最高的前N个IP

   前段时间面试中被问到如上问题,日常不怎么注意积累,以此谨记。

访问IP    页面
[nxuser@im440-zh test]$ vi log 135.252.172.181 page1 136.252.172.182 page1 135.252.172.181 page2 135.252.172.183 page1 136.252.172.184 page1 135.252.172.181 page3 135.252.172.185 page1 136.252.172.182 page2 135.252.172.183 page2 135.252.172.181 page4 136.252.172.182 page1 136.252.172.182 page1 136.252.172.182 page1 136.252.172.182 page1 136.252.172.182 page1 136.252.172.182 page1 136.252.172.182 page1 136.252.172.182 page1 136.252.172.182 page1 136.252.172.182 page1 136.252.172.182 page1 136.252.172.182 page1 136.252.172.182 page1
代码实现:
1
[nxuser@im440-zh test]$ awk '{print $1}' log|sort |uniq -c|sort -r -n -k 1|head -n 3

 结果:

1      15 136.252.172.182

2       4 135.252.172.181

3       2 135.252.172.183

   注意 第二sort-n ,否则出现如下结果:

1       4 135.252.172.181

2       2 135.252.172.183

3      15 136.252.172.182

 

你可能感兴趣的:(linux)