uniq
: Uniquify filesuniq
writes the unique lines in the given input, or
standard input if nothing is given or for an input name of
‘-’. Synopsis:
uniq [option]… [input [output]]
By default, uniq
prints its input lines, except that
it discards all but the first of adjacent repeated lines, so that
no output lines are repeated. Optionally, it can instead discard
lines that are not repeated, or all repeated lines.
The input need not be sorted, but repeated input lines are detected
only if they are adjacent. If you want to discard non-adjacent
duplicate lines, perhaps you want to use sort -u
.
See sort
: Sort text files.
Comparisons honor the rules specified by the LC_COLLATE
locale category.
If no output file is specified, uniq
writes to standard
output.
The program accepts the following options. Also see Common options.
Skip n fields on each line before checking for uniqueness. Use a null string for comparison if a line has fewer than n fields. Fields are a sequence of blank characters followed by non-blank characters. Field numbers are one based, i.e., -f 1 will skip the first field (which may optionally have leading blanks).
For compatibility uniq
supports a traditional option syntax
-n. New scripts should use -f n instead.
Skip n characters before checking for uniqueness. Use a null string for comparison if a line has fewer than n characters. If you use both the field and character skipping options, fields are skipped over first.
On systems not conforming to POSIX 1003.1-2001,
uniq
supports a traditional option syntax
+n.
Although this traditional behavior can be controlled with the
_POSIX2_VERSION
environment variable (see Standards conformance), portable scripts should avoid commands whose
behavior depends on this variable.
For example, use ‘uniq ./+10’ or ‘uniq -s 10’ rather than
the ambiguous ‘uniq +10’.
Print the number of times each line occurred along with the line.
Ignore differences in case when comparing lines.
Discard lines that are not repeated. When used by itself, this option
causes uniq
to print the first copy of each repeated line,
and nothing else.
Do not discard the second and subsequent repeated input lines, but discard lines that are not repeated. This option is useful mainly in conjunction with other options e.g., to ignore case or to compare only selected fields. The optional delimit-method, supported with the long form option, specifies how to delimit groups of repeated lines, and must be one of the following:
Do not delimit groups of repeated lines. This is equivalent to --all-repeated (-D).
Output a newline before each group of repeated lines. With --zero-terminated (-z), use a zero byte (ASCII NUL) instead of a newline as the delimiter.
Separate groups of repeated lines with a single newline. This is the same as using ‘prepend’, except that no delimiter is inserted before the first group, and hence may be better suited for output direct to users. With --zero-terminated (-z), use a zero byte (ASCII NUL) instead of a newline as the delimiter.
Output is ambiguous when groups are delimited and the input stream contains empty lines. To avoid that, filter the input through ‘tr -s '\n'’ to remove blank lines.
This is a GNU extension.
Output all lines, and delimit each unique group. With --zero-terminated (-z), use a zero byte (ASCII NUL) instead of a newline as the delimiter. The optional delimit-method specifies how to delimit groups, and must be one of the following:
Separate unique groups with a single delimiter. This is the default delimiting method if none is specified, and better suited for output direct to users.
Output a delimiter before each group of unique items.
Output a delimiter after each group of unique items.
Output a delimiter around each group of unique items.
Output is ambiguous when groups are delimited and the input stream contains empty lines. To avoid that, filter the input through ‘tr -s '\n'’ to remove blank lines.
This is a GNU extension.
Discard the last line that would be output for a repeated input group.
When used by itself, this option causes uniq
to print unique
lines, and nothing else.
Compare at most n characters on each line (after skipping any specified fields and characters). By default the entire rest of the lines are compared.
Delimit items with a zero byte rather than a newline (ASCII LF). I.e., treat input as items separated by ASCII NUL and terminate output items with ASCII NUL. This option can be useful in conjunction with ‘perl -0’ or ‘find -print0’ and ‘xargs -0’ which do the same in order to reliably handle arbitrary file names (even those containing blanks or other special characters). With -z the newline character is treated as a field separator.
An exit status of zero indicates success, and a nonzero value indicates failure.