[Shell] dataset and difference set of data

Source: Internet
Author: User

[Shell] dataset and difference set of data

Statistical data often requires files A and B. Now you need to know the data in the file, which are shared by A and B, which only appear in file A (or those only appear in file B ). You can use the uniq command to complete this requirement.

Uniq-d will output duplicate rows

Uniq-u only displays unique rows

You can use uniq sort to slightly execute subtotal.

For example, file

100101102100
File B
103102102

The script is as follows:

Cat A | sort | uniq> tmpA # A dataset deduplication cat B | sort | uniq> tmpB # B dataset deduplication cat tmpA tmpB | sort | uniq-d # A and B intersection cat tmpA tmpB | sort | uniq-u # data in A and in B (A-B)

What is the Computing Principle of intersection and difference set? In fact, it is clear to execute it in one step.

Cat A | sort | uniq> tmpA; cat tmpA100101102cat B | sort | uniq> tmpB; cat tmpB102103
Then combine two files with cat tmpA tmpB
Cat tmpA tmpB100101102102103

Finally, use uniq to find the duplicate rows in the file, that is, the intersection of A and B.

Cat tmpA tmpB | sort | uniq-d102
As for the difference set, the principle is similar.

Related Article

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.