uniq -c: Count Repeated Lines
uniq -c collapses adjacent duplicate lines and prefixes each with its count.
uniq -c turns a list into a tally. It only counts adjacent runs, so the input almost always needs sorting first.
What it does
uniq -c collapses each run of consecutive identical lines into one line, prefixed by the run length. Because it only looks at adjacent lines, identical lines that are not next to each other are counted as separate runs unless the input is sorted.
Common usage
sort access.log | uniq -c | sort -nr | head # top lines
cut -d' ' -f1 access.log | sort | uniq -c # hits per IP
sort -u list.txt | uniq -cOptions
| Flag | What it does |
|---|---|
| -c | Prefix each line with its occurrence count |
| -d | Only print lines that repeated |
| -u | Only print lines that did not repeat |
| -i | Ignore case when comparing |
| -f N | Skip the first N fields when comparing |
In CI
The canonical frequency report is sort | uniq -c | sort -nr. Always sort first; uniq -c on unsorted input produces a count per adjacent run, not per distinct value.
Common errors in CI
Counts look too high because the input was not sorted, so the same value appears in several runs; pipe through sort first. The count field is right-justified with leading spaces, which can break a later cut -d' '; use awk or tr -s ' ' to normalize before splitting.