在Linux中的文本文件中的IP“按计数”

我有一个包含IP地址的大文本文件:

123.33.22.33 221.23.128.2 123.33.22.33 92.222.192.12 92.222.192.12 123.33.22.33 

我可以把它分类为:

 123.33.22.33 123.33.22.33 123.33.22.33 221.23.128.2 92.222.192.12 92.222.192.12 

(裸眼看)第一次IP发生三次,第二次,最后一次发生两次。

我希望能够在巨大的日志文件中做到这一点,显然是以一种自动的方式。 可能吗?

谢谢

 cat file_with_ips | sort -nr | uniq -c | sort -nr -k 1 

将按ip计数sortingdesc,显示第一列的计数器

例如

 root@pinkpony:~# cat /tmp/xx 123.33.22.33 221.23.128.2 123.33.22.33 92.222.192.12 92.222.192.12 123.33.22.33 root@pinkpony:~# cat /tmp/xx | sort -nr | uniq -c | sort -nr -k1 3 123.33.22.33 2 92.222.192.12 1 221.23.128.2 root@pinkpony:~# 

一旦你有它的sorting,你可以通过uniq -cpipe道

 cat sorted.list | uniq -c 2 1.2.3.4 1 5.6.7.8 

这会为你计数。

您可以使用uniq -c来获得计数,但是一定要先对input进行sorting,否则您只能得到连续的相似行的计数:

 sort yourfile | uniq -c 

我经常用最匹配的线条来sorting:

 sort yourfile | uniq -c | sort -nr