现在的位置: 首页 > 综合 > 正文

用awk一些常用技巧sort uniq

2018年08月30日 ⁄ 综合 ⁄ 共 807字 ⁄ 字号 评论关闭

统计文件中第一列中同一IP出现的次数
cat test

123.122.123.12 12121212
121.2332.121.11 232323
255.255.255.255 21321
123.122.123.12 12121212
123.122.123.12 1212121er2
123.122.123.12 12121212eer
123.122.123.12 12121212ere
255.255.255.255 21321
121.2332.121.11 232323
255.255.255.255 21321
命令
awk '{name[$1]++ }; END {for (count in name) print count,name[count]}' test|sort
输出:

121.2332.121.11 2
123.122.123.12 5
255.255.255.255 3
按第两列降序排序
awk '{name[$1]++ }; END {for (count in name) print count,name[count]}' test|sort -k 2 -rn
输出:
123.122.123.12 5
255.255.255.255 3
121.2332.121.11 2
注:-k为排序关键列 
    -r为降序排序
    -n按算术值对数字字段排序。数字字段可包含前导空格、可选减号、十进制数字、千分位分隔符和可选基数符。对包含任何非数字字符的字段进行数字排序会出现无法预知的结果。
也可用
awk '{print $1}' test|sort|uniq -c
输出:

      2 121.2332.121.11
      5 123.122.123.12
      3 255.255.255.255
如果要IP在前面
则awk '{print $1}' test|sort|uniq -c|awk '{print $2,$1}'
输出:
121.2332.121.11 2
123.122.123.12 5
255.255.255.255 3

抱歉!评论已关闭.