如何使用 bash 在 linux 中按出现次数对字符串进行排序?
How to sort strings by number of occurrences with bash in linux?
我有一个包含如下行的文件:
May 25 05:34:16 192.0.2.2
May 25 05:34:16 192.0.2.1
May 25 05:34:16 192.0.1.5
May 25 05:38:16 192.0.2.2
现在我想获取 IP 的出现次数并按出现次数最多以及出现频率列出它们,如下所示:
2 May 25 05:34:16 192.0.2.2
1 May 25 05:34:16 192.0.2.1
1 May 25 05:34:16 192.0.1.5
最好在 bash 中用 awk 写一行。
使用 GNU 排序和 GNU uniq:
sort -k4 file | uniq --count --skip-fields=3
输出:
1 May 25 05:34:16 192.0.1.5
1 May 25 05:34:16 192.0.2.1
2 May 25 05:34:16 192.0.2.2
参见:man uniq
我有一个包含如下行的文件:
May 25 05:34:16 192.0.2.2
May 25 05:34:16 192.0.2.1
May 25 05:34:16 192.0.1.5
May 25 05:38:16 192.0.2.2
现在我想获取 IP 的出现次数并按出现次数最多以及出现频率列出它们,如下所示:
2 May 25 05:34:16 192.0.2.2
1 May 25 05:34:16 192.0.2.1
1 May 25 05:34:16 192.0.1.5
最好在 bash 中用 awk 写一行。
使用 GNU 排序和 GNU uniq:
sort -k4 file | uniq --count --skip-fields=3
输出:
1 May 25 05:34:16 192.0.1.5 1 May 25 05:34:16 192.0.2.1 2 May 25 05:34:16 192.0.2.2
参见:man uniq