Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say I have a file which contains:


I want to have the output like this:

A 3
B 1
CC 1
share|improve this question
up vote 18 down vote accepted

I figured it out; one of uniq's options is -c, for "prefix lines by the number of occurrences":

$ uniq -c
share|improve this answer
Note that puts the numbers first. If you were fussy about the order, you could do: uniq -c filename.txt | sed 's/[^0-9]*\\([0-9]\+\\) \\(.*\\)/\2 \1/' – frabjous Jan 7 '11 at 3:28
Also note that it only counts adjacent repeat lines. A common idiom is sort | uniq – Steven D Jan 7 '11 at 3:54
uniq also puts the count in front of the datum. The original question would actually need something like this: sort filename | uniq -c | awk '{print $2, $1}' – Bruce Ediger Jan 7 '11 at 13:25

I just came here with a similar problem. From this, I managed to put together a slightly more advanced command, which I hope is useful for others.

As Steven D said in the comments above uniq only counts adjacent repeat lines, so you need to sort the lines first. After that we find the unique lines then sort again so the most occurring lines are on top.

sort file.txt | uniq -c | sort -nr > output.txt

Output is redirected into the file output.txt. If you just want to view results on the command line, remove the redirection and change the last command to sort -n so that the most common line will be at the bottom, i.e. definitely still on screen.

share|improve this answer
You can replace cat file.txt | sort with just sort file.txt. :) – mattdm Mar 7 '11 at 21:21
@mattdm: thanks, updated! – DisgruntledGoat Mar 8 '11 at 21:56
@mattdm: the downside of that formulation is that you can't quickly replace the cat with something more interesting. Since, you know, there is no cat. – SamB Nov 18 '11 at 21:34
@SamB Then write it as < file.txt sort | uniq -c. This is easy to edit, and still avoids the unnecessary cat. – hvd May 12 '15 at 9:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.