Bash tip: Find the intersection of sets, the set, the difference set, the symmetric difference set

Source: Internet
Author: User

It's easier to turn on the internet than to use awk.


Given two files a.txt and b.txt, each row is a record (assuming there are no duplicates), the intersection of two sets is required, the set of sets, the difference set, and the output results include only the unique items. The intersection is defined as a record item that appears simultaneously in two files. The set is defined as a record item that appears in any file, and the difference set (A-B) is defined as a record that appears in a and does not appear in B, and the symmetric difference is defined as a record that appears only in one file.
Suppose A.txt includes a, C, b three lines. Suppose B.txt includes D, E, C, b four lines.

Intersection, the two files are sorted together, with items that are output more than once:
$ Sort A.txt B.txt | Uniq-d
B
C

and set, the two files are sorted together, the duplicate items are counted only once:
$ Sort A.txt B.txt | Uniq
A
B
C
D
E

Chage (A-b), the elements of B repeat 2 and a of the elements are sorted together, output only one occurrence of the item:
$ sort A.txt B.txt B.txt | Uniq-u
A

Symmetric error, the two files are sorted together, outputting only the items that appear once:
$ Sort A.txt B.txt | Uniq-u
A
D
E

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.