Calan
Calan

Reputation: 13

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

fileA:

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

fileB:

2250630000000
2250670000000
2250700000000
2250760000000

I want to check if numberY (column 4) from fileA exists in fileB (which just has 1 column no ;), if not then I want the whole line of fileA printed, so not the matches.

so it should show

08-03-2021;5618452029952;TEXT;2250660000000

I thought I was close getting this to work with awk but am missing something somewhere.

Thank you for your answers.

addon: I started of by seeing if I could find the matches but even that seems to fail:

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

addon: added sample file

Upvotes: 1

Views: 1041

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133458

With your shown samples, could you please try following. Written and tested in GNU awk.

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

Explanation: Adding detailed explanation for above.

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.

Upvotes: 3

Related Questions