Linux Comm Command to find the intersection of the file, the difference set

Source: Internet
Author: User

A () and B (3,4,5), the intersection of A and B is that the difference of 3,a to B is 1 and the difference of 2,b to A is 4 and the result of the difference between 5,a and B is 1, 2, 4, 5.

You can use the Comm command to find these sets in Linux.

cat <<eof>set1.txt> orange> gold> Apple>  Sliver> steel> iron> EOF
cat <<eof>set2.txt> orange> gold> cookiee>  Carrot> EOF

Use the Comm command.

[email protected] tmp]# Comm set1.txt set2.txtapple                 file 1 inch  file2 in sorted order                Gold        Cookiee        Carrotsilversteeliron

The hints are not sorted, so the Comm must ensure that the compared files are ordered.

sort Set1.txt-o set1.txt; sort Set2.txt-o set2.txt
[Email protected] tmp]# Comm set1.txt set2.txt                             Apple        carrot        cookiee                goldiron                orangesilversteel

3 columns are output in the results, and each column is separated with a tab \ t. The first column is Set1.txt and Set2.txt, the second column is set2.txt and Set1.txt does not, and the third column is Set1.txt and Set2.txt.

The intersection, the difference set and the difference can be obtained according to the three columns.

The intersection is the third column. Use 1 and 2 to delete the first second column is the result of the third column.

[Email protected] tmp]# Comm set1.txt set2.txt-1 -2goldorange

The difference set of a to B is the first column, and the difference between B and A is the second column.

[Email protected] tmp]# Comm set1.txt set2.txt  -2 -3  # A to B difference set Appleironsilversteel
[Email protected] tmp]# Comm set1.txt set2.txt  -1 -3   # b difference set for a carrotcookiee

The difference between A and B is the combination of the first and second columns.

[Email protected] tmp]# Comm set1.txt set2.txt  -3  Apple        Carrot        Cookieeironsilversteel

However, the results of the two columns are not easy to view and should be processed so that they appear on the same column.

[[Email protected] tmp]# Comm set1.txt set2.txt  -3tr"\ t""  / " Applecarrotcookieeironsilversteel

Back to series article outline:http://www.cnblogs.com/f-ck-need-u/p/7048359.html

Reprint Please specify source:http://www.cnblogs.com/f-ck-need-u/p/7454694.html Note: If you think this article is not bad please click on the lower right corner of the recommendation, with your support to inspire the author more enthusiasm for writing, thank you very much!

Linux Comm Command to find the intersection of the file, the difference set

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.