系統(tǒng)提供了兩個(gè)待處理文件a.txt 和b.txt ,其中文件 a.txt 中的部分內(nèi)容如下: Hello My Name is Alice What is your name I am Bob I came from China Where are you from Oh my God
文件 b.txt 中的部分內(nèi)容如下: Alice is a good boy Bob is a nice man and he is one of my best friend God bless you
將文件 a.txt 中每一行的最后一個(gè)單詞作為集合 1 ;將文件 b.txt 中每一行的第一個(gè)單詞作為集合 2 ;請(qǐng)使用 shell 語(yǔ)言編寫(xiě)程序,輸出包含在集合 1 但不包含在集合 2 的所有元素。 注意事項(xiàng)禁止使用echo 手動(dòng)輸出或類似的方法手動(dòng)輸出差集。 # NR==FNR 第一個(gè)參數(shù)b.txt
# set[$1] 以第一列單詞為索引的數(shù)組
# !(NR==FNR) 不是第一個(gè)參數(shù)b.txt 也就是a.txt
# $NF in set 最后一列單詞包含在數(shù)組中
awk ' {if (NR==FNR) set[$1] = $1} {if(!(NR==FNR) && !($NF in set)) {print $NF}} ' b.txt a.txt
|