uniq - Report or filter out repeated lines in a file

Synopsis

uniq [-c | -du] [-f fields] [-s chars] [input_file [output_file]]

Description

The uniq utility reads the standard input comparing adjacent lines, and writes a copy of each unique input line to the standard output. The second and succeeding copies of identical adjacent input lines are not written. Repeated lines in the input will not be detected if they are not adjacent, so it may be necessary to sort the files first.

Options

-c
Precede each output line with the count of the number of times the line occurred in the input, followed by a single space.
-d
Suppress the writing of lines that are not repeated in the input.
-f fields
Ignore the first fields fields in each input line when doing comparisons. A field is a string of non-blank characters separated from adjacent fields by blanks. Field numbers are one based, so the first field is field one.
-s chars
Ignore the first chars characters in each input line when doing comparisons. If specified in conjunction with the -f option, the first chars characters after the first fields fields will be ignored. Character numbers are one based, so the first character is character one.
-u
Suppress the writing of lines that are repeated in the input.

Operands

If additional arguments are specified on the command line, the first such argument is used as the name of an input file, the second is used as the name of an output file.

Exit Status

Related information

Examples

In the following examples, the contents of example file are:

There are 5 apples
There are 9 oranges
There are 9 oranges
There are 2 pears
  1. Display the unique lines in the file "fruit".
    uniq fruit
                                 
    There are 5 apples
    There are 9 oranges
    There are 2 pears
    
  2. Display the lines that repeat in the file "fruit".
    uniq -d fruit
                             
    There are 9 oranges
    
  3. Display a list of how many times a line is repeated in the file "fruit".
    uniq -c fruit
                             
    1 There are 5 apples
    2 There are 9 oranges
    1 There are 2 pears