uniq joins lines that are the same and next to each other into one line. With -c, it also writes how many lines it joined.
text
learner@tidepool:~$ cut -d' ' -f2 visits.log | sort | uniq -c
2 ana
1 ben
1 chenSort first
uniq sees only the line just before. Lines that are the same but not next to each other are not joined:
| Input | uniq -c | sort | uniq -c |
|---|---|---|
ana, ben, ana | 1 ana, 1 ben, 1 ana | 2 ana, 1 ben |
sort puts the same lines next to each other. So always put sort before uniq.
Which is the most common?
This pipeline is a standard answer to "what happens most often?":
cut -d' ' -f2one column
sortsame names together
uniq -ccount each
sort -rnlargest count first
text
learner@tidepool:~$ cut -d' ' -f2 visits.log | sort | uniq -c | sort -rn
2 ana
1 chen
1 benAdd | head -n 3 for the top three.