shell bash 交集 並集 差集

2021-07-24 15:19:03 字數 1192 閱讀 4132

方法一(直接用檔名):取兩個文字檔案的並集、交集、差集

並:sort -m <(sort file1 | uniq) <(sort file2 | uniq) | uniq

交:sort -m <(sort file1 | uniq) <(sort file2 | uniq) | uniq -d

差 file1 - file2:

sort -m <(sort file1 | uniq) <(sort file2 | uniq) <(sort file2 | uniq) | uniq -u

方法二(用變數引數):取兩個文字檔案的並集、交集、差集

file1=***x

file2=yyyy

# 並:

sort -m <(sort $file1 | uniq) <(sort $file2 | uniq) | uniq

# 交:

sort -m <(sort $file1 | uniq) <(sort $file2 | uniq) | uniq -d

# 差 file1 - file2:

sort -m <(sort $file1 | uniq) <(sort $file2 | uniq) <(sort $file2 | uniq) | uniq -u

方法三:

file1=***x

file2=yyyy

# 並:

cat $file1 $file2 | sort | uniq

# 交:

cat $file1 $file2 | sort | uniq -d

備註:

uniq -d 會輸出重複行

uniq -u 只顯示唯一的行

grep命令

grep命令是常用來搜尋文字內容的,根據輸入的pattern,輸出命中的內容。可以利用它的檔案輸入pattern特性,來求兩個檔案的交集。

$ cd

e

那差集可以利用-v這個引數

,例如:

$ grep -f -v -f a.file b.filefg

$ grep -f -v -f b.file a.file

ab

其中第乙個命令求b-a,第二個命令求a-b

注意:1)grep求交集不要求輸入檔案是排序的,但最好是唯一的

2)差集時注意輸入檔案的順序

交集並集差集

1 內連線 select from student a inner join sc b on a.sno b.sno 左連線 select from student a left join sc b on a.sno b.sno 差集 select sno from student except s...

PHP 交集 並集 差集

array flip array splice 並集 array merge 過濾 array filter 去重 array unique 差集 array diff array diff 函式返回兩個陣列的差集陣列。該陣列包括了所有在被比較的陣列中,但是不在任何其他引數陣列中的鍵值。a1 arr...

python List交集 並集 差集

工作中遇到了求兩個集合的差集,但是集合集合中包含字典,所以使用difference方法會報錯,看了一些別人的部落格,整理了一下。1.獲取兩個list 的交集 print list set a intersection set b 2.獲取兩個list 的並集 print list set a uni...