echo Impossible|sed 's/Im/To be /'

October 16, 2010

Tip. Files intersection union difference

檔案內容的交集,聯集,差集

Files Sample :

$ cat file1
file1_only
We_are_the_same

$ cat file2
We_are_the_same
only_file2

Using Shell :

聯集 intersection
$ cat file1 file2 | sort | uniq
file1_only
only_file2
We_are_the_same

交集 union
$ cat file1 file2 | sort | uniq -d
We_are_the_same

差集 xor
$ cat file1 file2 | sort | uniq -u
file1_only
only_file2

file1 only, difference
$ cat file1 file2 file2 | sort | uniq -u
file1_only

file2 only, difference
$ cat file1 file2 file1 | sort | uniq -u
only_file2

Python code :

#!/usr/bin/python

f1=open("file1",'r').readlines()
f2=open("file2",'r').readlines()
sf1, sf2 = set(f1), set(f2)

print "difference", sf1.difference (sf2)
print "difference", sf2.difference (sf1)
print "intersection", sf1.intersection (sf2)
print "union", sf1.union (sf2)

Python Code exec Output :

difference set(['file1_only\n'])
difference set(['only_file2\n'])
intersection set(['We_are_the_same\n'])
union set(['only_file2\n', 'file1_only\n', 'We_are_the_same\n'])