文件中唯一行的出现次数

时间:2013-12-11 22:51:01

标签: bash unique rows

假设文件包含以下行:

aa bbb cccc dddd
eee ffff ggggg
aa bbb cccc dddd
i jj kkk llll
aa bbb cccc dddd
eee ffff ggggg

是否有任何bash命令来确定文件中的唯一行发生了多少次?输出应为:

aa bbb cccc dddd 3 time
eee ffff ggggg 2 times
i jj kkk llll 1 time

1 个答案:

答案 0 :(得分:2)

您可以组合使用几个命令:

sort <inputfile | uniq -c

uniq的手册页以:

开头
NAME
       uniq - report or omit repeated lines

SYNOPSIS
       uniq [OPTION]... [INPUT [OUTPUT]]

DESCRIPTION
       Discard all but one of successive identical lines from INPUT
       (or standard input), writing to OUTPUT (or standard output).

       -c, --count
              prefix lines by the number of occurrences

所以给定输入文件的输出是:

  3 aa bbb cccc dddd
  2 eee ffff ggggg
  1 i jj kkk llll