Here, OPTION, INPUT, and OUTPUT are optional. If you use only uniq command without any option or input/output filename then this command will apply on the standard input data. Many types of options can be used with this command to filter duplicate data in various ways from any text file. If you use an input file name with this command then the data will filter from that file. If you execute the command with the option, input filename, and output filename then the data will filter from input file based on the option and write the output into the output file.
Some major options of uniq command are discussed below.
- -f N or –skip-fields=N
It is used to skip N fields before detecting the uniqueness of data. Fields are the group of characters separated by whitespace or tab.
- -s N or –skip-chars=N
It is used to skip N characters before detecting the uniqueness of data.
- -w N or –check-chars=N
It is used to compare N characters only in a line.
- -c or –count
It is used to count how many times a line repeated in the searching data and the values are shown as the prefix of that line.
- -z or –zero-terminated
It is used to terminate the line with 0 bytes instead of using newline.
- -d or –repeated
It is used to print all repeated lines only.
- -D or –all-repeated[=METHOD]
It is used to print all repeated lines based on the used method. The following methods can be used with this option.
none: It is the default method and doesn’t delimit duplicate lines.
prepend: It adds a blank line before each set of duplicate lines.
separate: It adds a blank line between two duplicate lines.
- -u or –unique
It is used to print the unique lines only.
- -i or –ignore-case
It is used for case-insensitive comparison.
Examples of uniq command
Create a text file named uniq_test.txt with the following content:
I like PHP Programming
I like Java Programming
Example#1: Using -f option
The following command will apply uniq command by skipping first two fields of each line from uniq_test.txt file.
Example#2: Using -s option
The following command will apply uniq command by skipping 4 characters from each line of uniq_test.txt file.
Example#3: Using –w option
The following command will apply uniq command by comparing the first two characters of each line.
Example#4: Using –c option
The following command will count the appearance of each line in the file and displays the number at the front of each line of the output.
Example#5: Using –d option
The following command displays those lines from the file only that appeared multiple times in the file. Only one line has appeared two times in uniq_test.txt file which is displayed as output.
Example#6: Using –D option
The following command will print all duplicate lines from the file.
Example#7: Using –all-repeated option with prepend method
Three methods can be used with –all-repeated option which are mentioned earlier of this tutorial. Here, the prepend method is used with this option that prints duplicate lines by appending blank lines at the beginning of duplicate lines.
Example#8: Using –u option
The following command will find out all the unique lines from the file. There are three unique lines in uniq_test.txt file which are printed as output.
The uses of uniq command are explained and shown by using various examples in this tutorial. Hope, you will be able to use uniq command properly after reading this tutorial.