Find duplicate lines in a file and count how many time each line was duplicated?

user839145 picture user839145 · Jul 15, 2011 · Viewed 555.2k times · Source

Suppose I have a file similar to the following:

123 
123 
234 
234 
123 
345

I would like to find how many times '123' was duplicated, how many times '234' was duplicated, etc. So ideally, the output would be like:

123  3 
234  2 
345  1

Answer

wonk0 picture wonk0 · Jul 15, 2011

Assuming there is one number per line:

sort <file> | uniq -c

You can use the more verbose --count flag too with the GNU version, e.g., on Linux:

sort <file> | uniq --count