shell文件處理awk

系統提供了兩個待處理文件a.txtb.txt,其中文件 a.txt 中的部份內容以下:shell

 
 
 
 
HelloMy Name is AliceWhat is your nameI am BobI came from ChinaWhere are you fromOh my God

文件 b.txt 中的部份內容以下:數組

 
 
 
 
Alice is a good boyBob is a nice man and he is one of my best friendGod bless you

將文件 a.txt 中每一行的最後一個單詞做爲集合 1 ;將文件 b.txt 中每一行的第一個單詞做爲集合 2 ;請使用 shell 語言編寫程序,輸出包含在集合 1 但不包含在集合 2 的全部元素。less

注意事項

禁止使用echo手動輸出或相似的方法手動輸出差集。spa

# NR==FNR 第一個參數b.txt
# set[$1] 以第一列單詞爲索引的數組
# !(NR==FNR) 不是第一個參數b.txt 也就是a.txt
# $NF in set 最後一列單詞包含在數組中
awk  ' {if (NR==FNR) set[$1] = $1} {if(!(NR==FNR) && !($NF in set)) {print $NF}} ' b.txt a.txt
Hello
name
China
from
相關文章
相關標籤/搜索