commands

uniq — collapse adjacent repeated lines

Compare adjacent lines, not all lines. -c counts groups, -d prints repeated groups, -u prints groups occurring once. Sort first for global grouping if order may change. Example assumes a fresh text-streams lab fixture.

Formal Syntax & Structure

linux syntaxlinux
uniq [OPTION]... [INPUT [OUTPUT]]

Parameters & Attributes

NameTypeDescription
-coptionCount groups
-doptionRepeated groups
-uoptionNonrepeated groups
INPUToperandInput file
OUTPUToperandDistinct output file

Return Value

exit status and output

GNU uniq writes adjacent-group results and returns nonzero on read/write errors. With -d or -u, an empty result can be successful; compare the chosen group output.

Working Example

Example Snippetlinux
LC_ALL=C sort labels.txt | uniq -c
Linux Reference