Uniq

From ICO wiki
Revision as of 21:11, 5 December 2016 by Jkuzmin (talk | contribs)
Jump to navigationJump to search


Autor

Jevgeni Kuzmin, A21

16.11.2016

Description

uniq - report or filter out repeated lines in a file.

The uniq utility reads the specified input_file comparing adjacent lines, and writes a copy of each unique input line to the output_file. If input_file is a single dash (`-') or absent, the standard input is read. If output_file is absent, standard output is used for 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.

Synopsis

  uniq [-c | -d | -u] [-i] [-f num] [-s chars] [input_file [output_file]]

Options

  • -c, --count; - Precede each output line with the count of the number of times the line occurred in the input, followed by a single space.
  • -d , --repeated ; - Only output lines that are repeated in the input.
  • -D , --all-repeated; - Print all duplicate lines

delimit-method={none(default),prepend,separate} Delimiting is done with blank lines.

  • -f , --skip-fields=N ; - Ignore the first num 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, i.e., the first field is field one.
  • -i , --ignore-case ; - Case insensitive comparison of lines.
  • -s , --skip-chars=N ; - 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 num fields will be ignored. Character numbers are one based, i.e., the first character is character one.
  • -u , --unique ; - Only output lines that are not repeated in the input.
  • -w , --check-chars=N ; - Compare no more than N characters in lines.

Examples

Let's say we have an eight-line text file, myfile.txt, which contains the following text:

This is a line.
This is a line.
This is a line.

This is also a line.
This is also a line.

This is also also a line.

Here are several ways to run uniq on this file, and the output it creates:

  uniq myfile.txt

This is a line.

This is also a line.

This is also also a line.

  uniq -c myfile.txt

3 This is a line.
1
2 This is also a line.
1
1 This is also also a line.

  uniq -d myfile.txt

This is a line.
This is also a line.

  uniq -u myfile.txt

This is also also a line.

  uniq -D myfile.txt

This is a line.
This is a line.
This is a line.
This is also a line.
This is also a line.


Environment

The LANG, LC_ALL, LC_COLLATE and LC_CTYPE environment variables affect the execution of uniq as described in environ(7).

Exit status

The uniq utility exits 0 on success, and >0 if an error occurs.

Compatibility

The historic +number and -number options have been deprecated but are still supported in this implementation.

Standards

The uniq utility conforms to IEEE Std 1003.1-2001 (``POSIX.1) as amended by Cor. 1-2002.

History

A uniq command appeared in Version 3 AT&T UNIX.

Notes

uniq does not detect repeated lines unless they are adjacent. You may want to sort the input first, or use sort -u instead of uniq.

Related commands

comm — Compare two sorted files line by line.
pack — Compress files using a Huffman algorithm.
pcat — Print the uncompressed contents of a compressed file.
sort — Sort the lines in a text file.
uncompress — Extract files from compressed archives.

References

http://www.thegeekstuff.com/2013/05/uniq-command-examples
https://www.freebsd.org/cgi/man.cgi?query=uniq&sektion=1
https://web.archive.org/web/20051028205957/http://www.linuxmanpages.com/man1/uniq.1.php
http://www.computerhope.com/unix/uuniq.htm