Reports or deletes repeated lines in a file.
uniq [ -c | -d | -u ] [ -f Fields ] [ -s Characters ] [ -Fields ] [ +Characters ] [ InFile [ OutFile ] ]
The uniq command deletes repeated lines in a file. The uniq command reads either standard input or a file specified by the InFile parameter. The command first compares adjacent lines and then removes the second and succeeding duplications of a line. Duplicated lines must be adjacent. (Before issuing the uniq command, use the sort command to make all duplicate lines adjacent.) Finally, the uniq command writes the resultant unique lines either to standard output or to the file specified by the OutFile parameter. The InFile and OutFile parameters must specify different files.
The input file must be a text file. A text file is a file that contains characters organized into one or more lines. The lines can neither exceed 2048 bytes in length (including any newline characters) nor contain null characters.
The uniq command compares entire lines by default. If the -f Fields or -Fields flag is specified, the uniq command ignores the number of fields specified by the Fields variable. A field is a string of characters separated from other character strings by one or more <blank> characters. If the -s Characters or -Characters flag is specified, the uniq command ignores the number of characters specified by the Characters variable. Values specified for the Fields and Characters variables must be positive decimal integers.
The current national language environment determines the <blank> characters used by the -f flag as well as how the -s flag interprets bytes as a character.
The uniq command exits with a value of 0 if successful. Otherwise, it exits with a value greater than 0.
This command returns the following exit values:
0 | The command ran successfully. |
>0 | An error occurred. |
To delete repeated lines in a file named fruit and save it to a file named newfruit, enter:
uniq fruit newfruit
If the fruit file contains the following lines:
apples apples peaches pears bananas cherries cherries
then the newfruit file will contain the following lines after you run the uniq command:
apples peaches pears bananas cherries
/usr/bin/uniq | Contains the uniq command. |
The comm command, sort command.