Skip to content
Latchkey

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

Terminal
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 -c

Options

FlagWhat it does
-cPrefix each line with its occurrence count
-dOnly print lines that repeated
-uOnly print lines that did not repeat
-iIgnore case when comparing
-f NSkip 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.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →