[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Commands Reference, Volume 5

uniq Command

Purpose

Reports or deletes repeated lines in a file.

Syntax

uniq-c | -d | -u ] [  -f Fields ] [  -s Characters ] [  -Fields ] [ +Characters ] [ InFile OutFile ] ]

Description

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.

Flags

-c Precedes each output line with a count of the number of times each line appeared in the input file.
-d Displays only the repeated lines.
-f Fields Ignores the number of fields specified by the Fields variable. If the value of the Fields variable exceeds the number of fields on a line of input, the uniq command uses a null string for comparison. This flag is equivalent to the -Fields flag.
-u Displays only the unrepeated lines.
-s Characters Ignores the number of characters specified by the Characters variable. If the value of the Characters variable exceeds the number of characters on a line of input, the uniq command uses a null string for comparison. If both the -f and -s flags are specified, the uniq command ignores the number of characters specified by the -s Characters flag starting in the field following the fields specified by the -f Fields flag. This flag is equivalent to the +Characters flag.
-Fields Ignores the number of fields specified by the Fields variable. This flag is equivalent to the -f Fields flag.
+Characters Ignores the number of characters specified by the Characters variable. If both the -Fields and +Characters flags are specified, the uniq command ignores the number of characters specified by the +Characters flag starting in the field following the fields specified by the -Fields flag. This flag is equivalent to the -s Characters flag.

Exit Status

This command returns the following exit values:

0 The command ran successfully.
>0 An error occurred.

Example

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

Files

/usr/bin/uniq Contains the uniq command.

Related Information

The comm command, sort command.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]