2013-12-11 21 views
1

假设一个文件有以下行:在一个文件中的唯一行发生次数

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 

回答

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 
+0

它是possi可以将出现次数作为后缀,而不是以逗号作为前缀吗? – Reza

+0

是的。 'uniq'命令本身不会这样做,但是你可以使用'sed'或'awk'这样的命令轻松完成。 –

相关问题