How to Use uniq to remove duplicate commands

Source: Internet
Author: User

Original article address

Linux Shell learning: How to Use the uniq command to introduce the function of the uniq command: display unique rows, only once for those repeated rows! Next we will explain through practical examples. [keyword] Linux Shell uniq

Check the content of the test.txt file and you can see the repeated rows in the file.

Root@hexu.org ~ #Cat test.txt
Boy took bat home
Boy took bat home
Girl took bat home
Dog brought hat home
Dog brought hat home
Dog brought hat home


The uniq command only displays duplicate rows once without adding any parameters.

Root@hexu.org ~ #Uniq test.txt
Boy took bat home
Girl took bat home
Dog brought hat home


-The C parameter shows the number of consecutive occurrences of each row in the file.

Root@hexu.org ~ #Uniq-C test.txt
2 boy took bat home
1 girl took bat home
3 dog brought hat home


-D option only displays the rows that repeatedly appear in the file.

Root@hexu.org ~ #Uniq-D test.txt
Boy took bat home
Dog brought hat home


-U option shows that there are no consecutive rows in the file.

Root@hexu.org ~ #Uniq-u test.txt
Girl took bat home


Ignore the first two fields of each line, ignore the first character of the second blank character and the third field, and the result is at home

Root@hexu.org ~ #Uniq-F 2-S 2 test.txt
Boy took bat home


Ignore the first field of each line. In this way, the line starting with "boy" and "girl" appears to be a row that repeats continuously.

Root@hexu.org ~ #Uniq-F 1 test.txt
Boy took bat home
Dog brought hat home


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.