Skip to content

uniq

9 commands

Filters or counts adjacent duplicate lines — usually paired with sort.

  • text processing
  • counting

Install uniq

sudo apt install coreutils

Basics6

sort <file> | uniq

Removes duplicate lines. uniq only compares neighbours, so sort first.

Link
sort <file> | uniq -c

Prefixes each line with how many times it occurs.

Link
sort <file> | uniq -c | sort -rn

Frequency ranking — the most common lines first.

Link
sort <file> | uniq -d

Prints only lines that appear more than once.

Link
sort <file> | uniq -u

Prints only lines that appear exactly once.

Link
sort <file> | uniq -i

Ignores case when comparing.

Link

Fields3

uniq -f <n> <file>

Skips the first n fields when comparing — useful when lines start with timestamps.

Link
uniq -s <n> <file>

Skips the first n characters when comparing.

Link
uniq -w <n> <file>

Compares only the first n characters.

Link