簡體   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