Text
uniq
Filter adjacent duplicate lines.
textduplicateuniquecountfilter
Additional Notes
uniq removes or reports adjacent duplicate lines. It only compares neighboring lines, so input is usually sorted first.
Use sort | uniq to find unique lines across a whole file or stream.
Syntax
uniq [options] [input [output]]
Parameters
options: Flags that change howuniqbehaves.file: Text file to read or process.
Common Options
-c,--count: Prefix lines with occurrence counts.-d,--repeated: Print only duplicate lines.-u,--unique: Print only lines that are not duplicated.-i,--ignore-case: Ignore case differences.-f N: Skip firstNfields.-s N: Skip firstNcharacters.
Examples
sort names.txt | uniq
Print unique names.
sort access.log | uniq -c
Count repeated lines.
sort errors.txt | uniq -d
Show only lines that appear more than once.
sort words.txt | uniq -u
Show only lines that appear once.
sort names.txt | uniq -ci
Count duplicates while ignoring case.
Practical Notes
uniqchecks adjacent lines only.- Use
sortfirst unless you intentionally care about neighboring duplicates. sort | uniq -c | sort -nris useful for frequency counts.- For field-aware processing, consider
awk.