Uniq: Difference between revisions

From ICO wiki
Jump to navigationJump to search
Jkuzmin (talk | contribs)
No edit summary
Jkuzmin (talk | contribs)
Line 55: Line 55:
   uniq -u myfile.txt
   uniq -u myfile.txt
This is also also a line.
This is also also a line.


==What is the uniq command in UNIX?==
==What is the uniq command in UNIX?==

Revision as of 19:20, 5 December 2016


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.

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
-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.

What is the uniq command in UNIX?

The uniq command in UNIX is a command line utility for reporting or filtering repeated lines in a file. It can remove duplicates, show a count of occurrences, show only repeated lines, ignore certain characters and compare on specific fields. The command expects adjacent comparison lines so it is often combined with the sort command.


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.