linux shell求兩個文件的差、並、交集

比較兩個文件
a.txt:
1
1
2
3
4
1

b.txt:
5
6
1
2
1

#使用命令comm比較,輸入文件必須爲有序
#列之間使用製表符\t分隔
comm <(sort a.txt) <(sort b.txt)
1
1
1
2
3
4
5
6
#第一列爲a與b的差集 :a.txt -b.txt
comm <(sort a.txt) <(sort b.txt) | awk -F"\t" ‘{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲' | sed -e '/^/d’
#第二列爲b與a的差集:b.txt-a.txt
comm <(sort a.txt) <(sort b.txt) | awk -F"\t" ‘{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲' | sed -e '/^/d’
#第三列爲a與b的交集:a.txt & b.txt
comm <(sort a.txt) <(sort b.txt) | awk -F"\t" ‘{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 3}̲' | sed -e '/^/d’
#所有字符爲a與b的並集合:
comm <(sort a.txt) <(sort b.txt) | sed -e ‘s/\t//g’

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章