美文网首页
日志处理cat、awk、sort、uniq、head多命令使用

日志处理cat、awk、sort、uniq、head多命令使用

作者: 吕艳凯 | 来源:发表于2020-04-02 14:57 被阅读0次

    需求:根据文件内容,取出现次数最多的前两位ip
    基本文件内容查看:

    $cat test.txt 
    210.242.125.35 adwords.google.com
    212.188.10.167 www.google.com
    193.192.250.158 images-pos-opensocial.googleusercontent.com
    193.192.250.158 clients4.google.com
    222.255.120.42 google.com
    222.255.120.42 apis.google.com
    193.192.250.158 clients2.google.com
    193.192.250.158 clients3.google.com
    64.233.181.49 mts0.google.com
    64.233.181.49 maps.gstatic.com
    

    仅输出ip:

    $cat test.txt|awk '{print $1}'
    210.242.125.35
    212.188.10.167
    193.192.250.158
    193.192.250.158
    222.255.120.42
    222.255.120.42
    193.192.250.158
    193.192.250.158
    64.233.181.49
    64.233.181.49
    

    按字符串规则排序:

    $cat test.txt|awk '{print $1}'|sort
    193.192.250.158
    193.192.250.158
    193.192.250.158
    193.192.250.158
    210.242.125.35
    212.188.10.167
    222.255.120.42
    222.255.120.42
    64.233.181.49
    64.233.181.49
    

    去重计数:

    $cat test.txt|awk '{print $1}'|sort|uniq -c
          4 193.192.250.158
          1 210.242.125.35
          1 212.188.10.167
          2 222.255.120.42
          2 64.233.181.49
    

    按首列数字倒叙排列输出:
    sort -n表示计数从小到大输出 sort -r表示倒序输出

    $cat test.txt|awk '{print $1}'|sort|uniq -c|sort -nr
          4 193.192.250.158
          2 64.233.181.49
          2 222.255.120.42
          1 212.188.10.167
          1 210.242.125.35
    

    取对应行数:

    $cat test.txt|awk '{print $1}'|sort|uniq -c|sort -nr|head -n 2
          4 193.192.250.158
          2 64.233.181.49
    

    第二种去重计数方式:
    采用awk数组的形式计数,然后排序输出内容

    $cat test.txt|awk '{a[$1]++}END{for(i in a) print a[i],i}'|sort -nr|head -n 2
    4 193.192.250.158
    2 64.233.181.49
    

    相关文章

      网友评论

          本文标题:日志处理cat、awk、sort、uniq、head多命令使用

          本文链接:https://www.haomeiwen.com/subject/cfsxphtx.html