如何在Linux中使用bash按出现次数对字符串排序?

cre*_*row 1 sorting bash awk text-processing

我有一个包含以下内容的文件:

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
Run Code Online (Sandbox Code Playgroud)

现在,我想获取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
Run Code Online (Sandbox Code Playgroud)

最好是一个带有awk的bash衬垫。

Cyr*_*rus 5

使用GNU sort和GNU uniq:

sort -k4 file | uniq --count --skip-fields=3
Run Code Online (Sandbox Code Playgroud)

输出:

      1 May 25 05:34:16 192.0.1.5
      5月1日25:34:16 192.0.2.1
      5月2日05:34:16 192.0.2.2

看到: man uniq