系統提供了兩個待處理檔案a.txt和b.txt,其中檔案 a.txt 中的部分內容如下:
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 的所有元素,
注意事項
禁止使用echo手動輸出或類似的方法手動輸出差集,
# 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/189212.html
標籤:Linux
下一篇:shell檔案處理awk
