Shell统计文件中出现次数最多的前10个

获取日志中requestKey出现的最多的前10

1
2
3
4
5
6
7
8
9
requestKey: /v2/publisher/slot/total
RequestParams: {"q":{"sids":[1774397970,42952954,-1149476927,1594194495,315027309,2062249474]}}
requestKey: /v2/publisher/slot/total
RequestParams: {"q":{"sids":[1774397970,42952954,-1149476927,1594194495,315027309,2062249474]}}
requestKey: /v2/publisher/slot/total
RequestParams: {"q":{"sids":[1774397970,42952954,-1149476927,1594194495,315027309,2062249474]}}
requestKey: /v2/publisher/media/list
RequestParams: {"enddate":"2017-03-01","limit":10,"order":"desc","period":"d","q":{"so":-496483550},"skip":0,"sort":"pv","startdate":"2017-02-01"}
......

  1. 使用uniq -c
    grep requestKey log4j.log|awk '{print $2}'|sort|uniq -c|sort -k1nr|head -10
    
  2. 使用awk数组统计
    grep requestKey log4j.log|awk '{arr[$2]++}END{for(k in arr){print arr[k],k}}'|sort -k1nr|head -10
    

这两种的最后都用到了比较关键的sort -k1nr
-k 标示用那个字段进行排序, -k1 标示用第一个字段, sort可以支持多个字段, -k1 -k2
-n 使用数字排序,默认使用的字符串排序.
-r 反向输出