awk cross-referencing between two files

I need to make my sloppy, existing code (non awk) more efficient and I seem to be terrible with awk :wall:

I've tried around quite a bit, maybe you guys can conjure up a quick solution...

file1:

2 SOL 563
      2 SOL 565
      3 SOL 589
      2 SOL 603
      1 SOL 612
      1 SOL 621
      2 SOL 623
3 SOL 770
      3 SOL 771
      3 SOL 772
      3 SOL 773
2 SOL 774

file 2:

ATOM  58262   OW SOL  18120     27.770  75.370 125.740
ATOM  58263  HW1 SOL  18120     28.020  75.170 124.790
ATOM  58264  HW2 SOL  18120     28.490  75.930 126.160
ATOM  58265   OW SOL  18121     28.420  85.640 128.910
ATOM  58266  HW1 SOL  18121     27.430  85.700 129.010
ATOM  58267  HW2 SOL  18121     28.650  84.840 128.350
ATOM  58268   OW SOL  18122     25.670  84.980 115.400
ATOM  58269  HW1 SOL  18122     25.530  85.050 116.390
ATOM  58270  HW2 SOL  18122     26.510  84.470 115.220
ATOM  58271   OW SOL  18123     22.720  82.610 124.230
ATOM  58272  HW1 SOL  18123     23.580  82.730 124.730
ATOM  58273  HW2 SOL  18123     22.300  81.740 124.500
ATOM  58274   OW SOL  18124     31.360  78.340 124.340
ATOM  58275  HW1 SOL  18124     31.570  79.080 124.980
ATOM  58276  HW2 SOL  18124     30.470  78.510 123.910

So basically what I need is this...

1) check every line of file1/column1 for values smaller than 3
2) if true then read value of file1/column3
3) cross-reference value of file1/column3 with value of file2/column5
4) if match found then delete line from file2

I've started with...

awk '{a[d++]=$1}END{for(i=1;i<=d;i++){ if ( a < 3 ) print a }}' < file1

...so basically I'm stuck with step 2) already.

Could you set me on the right track?

nawk 'FNR==NR {if($1<3) f1[$3];next} !($5 in f1)' file1 file2
1 Like