Home  >  Article  >  Database  >  按ip统计访问日志top

按ip统计访问日志top

WBOY
WBOYOriginal
2016-06-07 15:31:041103browse

命令1: Shell代码 time awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -5 78129 192.168.13.166 75165 192.168.12.52 58657 192.168.12.199 54146 192.168.13.183 34310 192.168.13.181 real 0m5.849s user 0m5.566s sys 0m0.285s 命令2

  命令1:

  Shell代码

  time awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -5

  78129 192.168.13.166

  75165 192.168.12.52

  58657 192.168.12.199

  54146 192.168.13.183

  34310 192.168.13.181

  real 0m5.849s

  user 0m5.566s

  sys 0m0.285s

  命令2:

  Shell代码

  time awk '{a[$1]++}END{for(i in a){printf("%d\t%s\n",a[i],i)}}' access.log | sort -nr | head -5

  78129 192.168.13.166

  75165 192.168.12.52

  58657 192.168.12.199

  54146 192.168.13.183

  34310 192.168.13.181

  real 0m0.626s

  user 0m0.549s

  sys 0m0.080s

  以上是两个统计top命令,对比结果一目了然。命令1方便,效率低;命令2麻烦,效率高。

  之所以想到对比这两个命令,源于一次面试,当时我的答案是命令2(在这之前一直习惯用命令2,确实没用过命令1)。对方随即对我给出的答案表示了质疑。首先质疑答案的正确性“这种方式不对吧?”。当我解释说我工作中确实这么用过的时候,又质疑了这个命令的效率“应该有更高效的方法!”。

  我其实要说的是,面试有时候不一定能考察出真正的问题,呵呵。

【责编:peter】

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Oracle树形查询Next article:Angel 2D