Bash uniq Command

29/12/2020
Linux users need to create or read the text file in regular basis for many purposes. A text file can contain different types of numeric and character data. Same data can be stored multiple times in a text file. Sometimes, you may require reading any text file by omitting duplicate lines of data. Bash uniq command is a useful command line utility tool that is used to read a text file by filtering or removing adjacent duplicate lines from the text file. uniq command is used to detect the adjacent lines from a file and write the content of the file by filtering the duplicate values or write only the duplicate lines into another file.

Syntax:

uniq [OPTION]  [ INPUT  [OUTPUT] ]

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.

Options:

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:

Bash Programming
Bash Programming
Python Programming
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.

$ uniq -f 2 uniq_test.txt

Example#2: Using -s option

The following command will apply uniq command by skipping 4 characters from each line of uniq_test.txt file.

$ uniq -s 4 uniq_test.txt

Example#3: Using –w option

The following command will apply uniq command by comparing the first two characters of each line.

$ uniq -w 2 uniq_test.txt

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.

$ uniq -c uniq_test.txt

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.

$ uniq -d uniq_test.txt

Example#6: Using –D option

The following command will print all duplicate lines from the file.

$ uniq -D uniq_test.txt

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.

$ uniq –all-repeated=prepend uniq_test.txt

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.

$ uniq -u uniq_test.txt

Conclusion

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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash History Search

We need to type various commands in the shell for various purposes. Sometimes we need to execute same commands multiple...
29/12/2020

Bash text and background printing in different colors

A terminal is a very important application for any Linux operating system. It is mainly used to execute different commands...
29/12/2020

Bash Echo Examples

Multiple built-in functions exist in bash to print the output into the terminal. ‘echo’ is one of the most used...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Lý do tại sao bạn nên sử dụng proxy khi truy cập web đen
27/02/2023

Các lỗi thường gặp khi sử dụng proxy và cách khắc phục chúng.
27/02/2023

Tác động của việc sử dụng proxy đến tốc độ kết nối internet của bạn.
27/02/2023

Các tiện ích và công cụ để quản lý proxy.
27/02/2023

Các cách để kiểm tra tốc độ và độ ổn định của proxy.
27/02/2023