A(1,2,3) and B(3,4,5),A and B The intersection of 3,A Yes B The difference set of is 1 and 2,B Yes A The difference set of is 4 and 5,A and B The result of the difference is 1,2,4,5.

stay Linux Can be used in comm Command to find these sets .
[[email protected] tmp]# cat <<eof>set1.txt > orange > gold > apple > sliver > steel >
iron> eof [[email protected] tmp]# cat <<eof>set2.txt > orange > gold > cookiee >
carrot> eof
use comm command .
[[email protected] tmp]# comm set1.txt set2.txt apple orange comm: file 1 is not in
sorted order comm:file 2 is not in sorted order gold cookiee carrot silver
steel iron
Prompt is not sorted , therefore comm It is necessary to ensure that the comparison documents are in order .
[[email protected] tmp]# sort set1.txt -o set1.txt;sort set2.txt -o set2.txt
[[email protected] tmp]# comm set1.txt set2.txt apple carrot cookiee gold iron orange
silver steel

Output in the result 3 column , Use tabs for each column \t separate . The first column is set1.txt And set2.txt Not in , The second column is set2.txt And set1.txt Not in , The third column is set1.txt and set2.txt All of them .

According to these three columns, we can find the intersection , Sum of difference sets .

Intersection is the third column . use -1 and -2 Deleting the first and second columns respectively is the result of the third column .
[[email protected] tmp]# comm set1.txt set2.txt -1 -2 gold orange
A Yes B The difference set of is the first column ,B Yes A The difference set of is the second column .
[[email protected] tmp]# comm set1.txt set2.txt -2 -3 # A Yes B Difference set of apple iron silver steel
[[email protected] tmp]# comm set1.txt set2.txt -1 -3 # B Yes A Difference set of carrot cookiee
A and B The difference of is the combination of the first column and the second column .
[[email protected] tmp]# comm set1.txt set2.txt -3 apple carrot cookiee iron silver
steel
But it's not convenient to view the results in two columns , They should be processed so that they appear in the same column .
[[email protected] tmp]# comm set1.txt set2.txt -3 | tr "\t" "\0" apple carrot
cookiee iron silver steel