统计文件中第一列中同一IP出现的次数
[root@iz2ze9jjf7ocengnnvmmgcz ~]# cat 2.txt
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
[root@iz2ze9jjf7ocengnnvmmgcz ~]#
命令:awk '{name[$1]++};END {for (count in name)print count,name[count]}' 2.txt |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]}' 2.txt |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}' 2.txt|sort|uniq -c
输出:
2 121.2332.121.11
5 123.122.123.12
3 255.255.255.255
如果想要IP在前面
则命令:awk '{print $1}' 2.txt|sort|uniq -c |awk '{print $2,$1}'
输出:
121.2332.121.11 2
123.122.123.12 5
255.255.255.255 3
对出现的域名进行统计:
cat access.log
http://www.etiantian.org/index.html
http://www.etiantian.org/1.html
http://post.etiantian.org/index.html
http://mp3.etiantian.org/index.html
http://www.etiantian.org/3.html
http://post.etiantian.org/2.html
命令:awk -F '[/]+' '{print $2}' access.log|sort|uniq -c|sort -rn -k1
3 www.etiantian.org
2 post.etiantian.org
1 mp3.etiantian.org