shell文件处理awk

系统提供了两个待处理文件a.txtb.txt,其中文件 a.txt 中的部分内容如下:

  1. Hello
  2. My Name is Alice
  3. What is your name
  4. I am Bob
  5. I came from China
  6. Where are you from
  7. Oh my God

文件 b.txt 中的部分内容如下:

  1. Alice is a good boy
  2. Bob is a nice man and he is one of my best friend
  3. 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
Hello
name
China
from
原文地址:https://www.cnblogs.com/zhangxuechao/p/13869446.html