Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Say I have a file which contains:

A
A
A
B
CC

I want to have the output like this:

A 3
B 1
CC 1
share|improve this question
add comment

2 Answers

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
3  
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
add comment
up vote 13 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
1  
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
8  
Also note that it only counts adjacent repeat lines. A common idiom is sort | uniq –  Steven D Jan 7 '11 at 3:54
3  
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
add comment

Your Answer

 
discard

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.