Back to the command reference

uniq

ShellFundamentals In simulator In real Linux

Collapse adjacent duplicate lines

Name: uniq = unique

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

uniq collapses **adjacent** duplicate lines into one. Since it only compares neighbours, input is usually sorted first; "sort | uniq -c | sort -rn" is the classic way to count occurrences.

Syntax

uniq [OPTION]... [INPUT [OUTPUT]]

Common options

-c / --countPrefix lines with their count
-d / --repeatedOnly print duplicated lines
-u / --uniqueOnly print unique lines
-i / --ignore-caseIgnore case when comparing
-f / --skip-fields NSkip the first N fields when comparing
-s / --skip-chars NSkip the first N characters when comparing
-w / --check-chars NCompare only the first N characters

Examples

  • $ sort data/fruits.txt | uniq

    Sort first, because uniq only merges adjacent lines

    Explain
  • $ sort data/fruits.txt | uniq -c | sort -rn

    Count each word, most frequent first

    Explain

Related