Issue with files when there is no data in a file.

I have two files

File1.txt
 
000199458
000199463
000200442
000200831
000200866
000201009
000201050
000201405
000201666
000201682
 
File2.txt
 
DHP5104   1BWEL9                             000199458
DHP5104   1BWELD                             000199463
DHP5104   1BWEK3                             000200442
DHP5104   1BWEK3                             000200831
DHP5104   1C1FZD                             000200866
DHP5104   1BWEK3                             000202405
DHP5104   1C1FXX                             000202707
DHP5104   1C1FZD                             000203202
DHP5104   1BWEL9                             000204450
DHP5104   1BWEL9                             000204453
XVP5104   1BWEL9                             000199458

I want to check each row in File1.txt with file2.txt. If match is found then the record needs to be deleted in file2.txt

 
Output.txt
DHP5104   1BWEK3                             000202405
DHP5104   1C1FXX                             000202707
DHP5104   1C1FZD                             000203202
DHP5104   1BWEL9                             000204450
DHP5104   1BWEL9                             000204453

I below code is used to get the above output.

awk 'FNR==NR {f1[$1];next} !($3 in f1)' file1.txt file2.txt

But this is not working when the File1.txt is empty. Can someone from this group tell me how to handle if there are no rows in File1.txt. i.e when the file1.txt is empty all the rows from file2.txt should be loaded.

Any help would be appriciated!!

Thanks,
Arjun

Try this instead:

awk 'FILENAME==ARGV[1]{f1[$1];next} !($3 in f1)' file1.txt file2.txt
1 Like

Or

awk 'NF==1 {f1[$1];next} !($3 in f1)' file1 file2
1 Like

Thanks buddy this is working.