统计文件中第一列中同一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
如下格式时
14:09:47,812DirectgetServerIPshdjshd.mp4|From:123.111.176.187|IPID:6|ServiceID:6|RoomID:6|Type:1|return:1
统计同一IP出现的次数
awk -F "[:|]" '{name[$5]++ }; END {for (count in name) print count,name[count]}' testd |sort -k 2 -rn
输出:
123.233.176.133 2
111.234.136.134 2
123.111.176.183 1
awk定义多个分隔符
awk命令行选项 -F"[:|]" 告诉awk | 和 : 都是字段分隔符
[root@wy bin]# cat te
weblogic:x:502:600:home/weblogic:bin/bash
[root@wy bin]# cat te | awk -F"[:|]" '{print $7}'
bin
转载于:https://blog.51cto.com/tianshili/1638703