The uniq
command¶
The uniq
command in Linux is a command line utility that reports or filters out the repeated lines in a file. In simple words, uniq
is the tool that helps you to detect the adjacent duplicate lines and also deletes the duplicate lines. It filters out the adjacent matching lines from the input file(that is required as an argument) and writes the filtered data to the output file .
Examples:¶
In order to omit the repeated lines from a file, the syntax would be the following:
uniq kt.txt
In order to tell the number of times a line was repeated, the syntax would be the following:
uniq -c kt.txt
In order to print repeated lines, the syntax would be the following:
uniq -d kt.txt
In order to print unique lines, the syntax would be the following:
uniq -u kt.txt
In order to allows the N fields to be skipped while comparing uniqueness of the lines, the syntax would be the following:
uniq -f 2 kt.txt
In order to allows the N characters to be skipped while comparing uniqueness of the lines, the syntax would be the following:
uniq -s 5 kt.txt
In order to to make the comparison case-insensitive, the syntax would be the following:
uniq -i kt.txt
Syntax:¶
uniq [OPTION] [INPUT[OUTPUT]]
Possible options:¶
Flag | Description | Params |
---|---|---|
-c | It tells how many times a line was repeated by displaying a number as a prefix with the line. | - |
-d | It only prints the repeated lines and not the lines which aren’t repeated. | - |
-i | By default, comparisons done are case sensitive but with this option case insensitive comparisons can be made. | - |
-f | It allows you to skip N fields(a field is a group of characters, delimited by whitespace) of a line before determining uniqueness of a line. | N |
-s | It doesn’t compares the first N characters of each line while determining uniqueness. This is like the -f option, but it skips individual characters rather than fields. | N |
-u | It allows you to print only unique lines. | - |
-z | It will make a line end with 0 byte(NULL), instead of a newline. | - |
-w | It only compares N characters in a line. | N |
--help | It displays a help message and exit. | - |
--version | It displays version information and exit. | - |