去哪网的一个面试题:shell统计日志中各IP 出现的次数

去哪网的一个面试题:shell统计日志中各IP 出现的次数

http://james-lover.iteye.com/blog/2105795

  去哪网的面试:数据量不大的话用awk最方便,但长时间没有用过了,忘记了awk数组的用法。

在这里复习一下。

假设数据格式为:

Java代码  收藏代码
  1. 178.60.128.31 www.google.com.hk  
  2. 193.192.250.158 www.google.com  
  3. 210.242.125.35 adwords.google.com  
  4. 210.242.125.35 accounts.google.com.hk  
  5. 210.242.125.35 accounts.google.com  
  6. 210.242.125.35 accounts.l.google.com  
  7. 64.233.181.49 www.google.com  
  8. 212.188.10.167 www.google.com  
  9. 23.239.5.106 www.google.com  
  10. 64.233.168.41 www.google.com  
  11. 62.1.38.89 www.google.com  
  12. 62.1.38.89 chrome.google.com  
  13. 193.192.250.172 www.google.com  
  14. 212.188.10.241 www.google.com  
  15. 37.228.69.57 www.google.com  
  16. 222.255.120.42 www.google.com  
  17. 222.255.120.42 www.gstatic.com  
  18. 212.188.10.167 www.googleapis.com  
  19. 64.233.181.49 www.googleapis.com  
  20. 64.233.181.49 fonts.googleapis.com  
  21. 193.192.250.158 plus.google.com  
  22. 193.192.250.158 talkgadget.google.com   
  23. 193.192.250.158 ssl.gstatic.com  
  24. 193.192.250.158 images-pos-opensocial.googleusercontent.com  
  25. 193.192.250.158 images1-focus-opensocial.googleusercontent.com  
  26. 193.192.250.158 images2-focus-opensocial.googleusercontent.com  
  27. 193.192.250.158 images3-focus-opensocial.googleusercontent.com  
  28. 193.192.250.158 images4-focus-opensocial.googleusercontent.com  
  29. 193.192.250.158 images5-focus-opensocial.googleusercontent.com  
  30. 193.192.250.158 images6-focus-opensocial.googleusercontent.com  
  31. 193.192.250.158 clients4.google.com  
  32. 222.255.120.42 google.com  
  33. 222.255.120.42 apis.google.com  
  34. 222.255.120.42 clients1.google.com  
  35. 193.192.250.158 clients2.google.com  
  36. 193.192.250.158 clients3.google.com  
  37. 193.192.250.158 clients5.google.com  
  38. 64.233.181.49 maps.google.com  
  39. 64.233.181.49 mts0.google.com  
  40. 64.233.181.49 maps.gstatic.com  

 

awk的统计代码:

Java代码  收藏代码
  1. awk '{arr[$1]++;}END{for(i in arr){print i , arr[i] }}' test.txt  

 

输出:

Java代码  收藏代码
  1. [blog@AY1310301904525972ddZ ~]$ awk '{arr[$1]++;}END{for(i in arr){print i , arr[i] }}' test.txt  
  2. 212.188.10.241 1  
  3. 64.233.168.41 1  
  4. 23.239.5.106 1  
  5. 193.192.250.158 15  
  6. 178.60.128.31 1  
  7. 37.228.69.57 1  
  8. 212.188.10.167 2  
  9. 193.192.250.172 1  
  10. 62.1.38.89 2  
  11. 64.233.181.49 6  
  12. 210.242.125.35 4  
  13. 222.255.120.42 5  

 

增加排序:

Java代码  收藏代码
  1. [blog@AY1310301904525972ddZ ~]$ awk '{arr[$1]++;}END{for(i in arr){print i , arr[i] }}' test.txt | sort -n -k 2  
  2. 178.60.128.31 1  
  3. 193.192.250.172 1  
  4. 212.188.10.241 1  
  5. 23.239.5.106 1  
  6. 37.228.69.57 1  
  7. 64.233.168.41 1  
  8. 212.188.10.167 2  
  9. 62.1.38.89 2  
  10. 210.242.125.35 4  
  11. 222.255.120.42 5  
  12. 64.233.181.49 6  
  13. 193.192.250.158 15  

 

 =============对网友:【hattah】 回答的补充===============

测试了两种方法的效率:

理论上sort排序数据量越大,速度越慢。

实测结果:

Java代码  收藏代码
  1. [blog@AY1310301904525972ddZ ~]$ time awk '{print $1}' test.txt |sort|uniq -c  
  2.    1380 178.60.128.31  
  3.   17312 193.192.250.158  
  4.    1160 193.192.250.172  
  5.    4640 210.242.125.35  
  6.    2320 212.188.10.167  
  7.    1160 212.188.10.241  
  8.    5734 222.255.120.42  
  9.    1160 23.239.5.106  
  10.    1160 37.228.69.57  
  11.    2320 62.1.38.89  
  12.    1160 64.233.168.41  
  13.    6894 64.233.181.49  
  14.   
  15. real    0m0.236s  
  16. user    0m0.228s  
  17. sys     0m0.004s  

 

Java代码  收藏代码
  1. [blog@AY1310301904525972ddZ ~]$  time  awk '{arr[$1]++;}END{for(i in arr){print i , arr[i] }}' test.txt | sort -n -k 2    
  2. 193.192.250.172 1160  
  3. 212.188.10.241 1160  
  4. 23.239.5.106 1160  
  5. 37.228.69.57 1160  
  6. 64.233.168.41 1160  
  7. 178.60.128.31 1380  
  8. 212.188.10.167 2320  
  9. 62.1.38.89 2320  
  10. 210.242.125.35 4640  
  11. 222.255.120.42 5734  
  12. 64.233.181.49 6894  
  13. 193.192.250.158 17312  
  14.   
  15. real    0m0.025s  
  16. user    0m0.022s  
  17. sys     0m0.001s  

posted on 2017-08-31 09:50  小西红柿  阅读(284)  评论(0)    收藏  举报

导航