系统提供了两个待处理文件a.txt 和b.txt ,其中文件 a.txt 中的部分内容如下: 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 中的部分内容如下: Alice is a good boy Bob is a nice man and he is one of my best friend God 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
|