繁体   English   中英

awk 检查文件 A 中列的值是否存在于文件 B 中,如果不存在则打印文件 A 的整行

[英]awk check if value from a column in file A exists in file B and if not print whole line of file A

文件A:

date;numberX;descX;numberY
08-03-2021;5618452029952;TEXT;2250630000000
08-03-2021;5618452029952;TEXT;2250660000000
08-03-2021;5618452029952;TEXT;2250670000000
08-03-2021;5618452029952;TEXT;2250700000000
08-03-2021;5618452029952;TEXT;2250760000000

文件B:

2250630000000
2250670000000
2250700000000
2250760000000

我想检查 fileA 中的 numberY (第 4 列)是否存在于 fileB 中(只有 1 列 no;),如果不存在,那么我想要打印 fileA 的整行,而不是匹配项。

所以它应该显示

08-03-2021;5618452029952;TEXT;2250660000000

我以为我很接近让它与 awk 一起工作,但在某处遗漏了一些东西。

谢谢您的回答。

插件:我首先查看是否可以找到匹配项,但即使这样似乎也失败了:

awk -F \; 'NR == FNR { a[$0]; next } ($4 in a)' fileB fileA

插件:添加示例文件

使用您显示的示例,您能否尝试以下操作。 在 GNU awk中编写和测试。

awk -v RS='\r?\n' 'FNR==NR{arr[$0];next} !($4 in arr)' fileB FS=";" fileA

说明:为上述添加详细说明。

awk -v RS='\r?\n' '    ##Starting awk program from here.
FNR==NR{               ##Checking condition which will be TRUE when fileB is being read.
  arr[$0]              ##Creating array arr with current line index.
  next                 ##next will skip all further statements from here.
}
!($4 in arr)           ##Checking condition if 4th column of fileA is present in arr then print line.
' fileB FS=";" fileA   ##Mentioning Input_file(s) and setting FS=";" before fileA.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM