uniq -c用于计数文件中的重复行不起作用

问题描述

我有一个名为test.txt的文件,其中包含以下文本。

red: 8-17-20
red: 8-17-20
red: 8-18-20
blue: 8-18-20
blue: 8-18-20
blue: 8-18-20
red: 8-18-20
red: 8-18-20
red: 8-19-20
red: 8-19-20
blue: 8-19-20
red: 8-19-20
blue: 8-19-20
blue: 8-19-20
blue: 8-18-20

关于如何计算重复行,我已经阅读了很多可能的答案,其中大多数指出我使用uniq -c

我已使用以下命令sort test.txt | uniq -c,得到的结果是

      4 blue: 8-18-20
      3 blue: 8-19-20
      2 red: 8-17-20
      3 red: 8-18-20
      3 red: 8-19-20

为什么我看不到

      2 red: 8-17-20
      3 red: 8-18-20
      3 blue: 8-18-20
      3 red: 8-19-20
      4 blue: 8-19-20

有人可以帮助我了解我在做什么错吗?

解决方法

您正在分类,但您也想在分类。

$ sort test.txt | uniq -c | sort -n
      2 red: 8-17-20
      3 blue: 8-19-20
      3 red: 8-18-20
      3 red: 8-19-20
      4 blue: 8-18-20

请注意,3 blue3 red之前排序。