compare files by numerical value

Hi everyone,

I would love to have a script that does the following:

I have one file that looks like that:

ATOM      1  BB  SER 1   1     -31.958 -25.125 -11.061  1.00  0.00      
ATOM      3  BB  GLY 1   2     -32.079 -26.085 -14.466  1.00  0.00      
ATOM      4  BB  VAL 1   3     -36.455 -21.265 -15.792  1.00  0.00      
ATOM      6  BB  SER 1   4     -37.401 -20.877 -19.029  1.00  0.00     
ATOM      8  BB  ALA 1   5     -42.701 -21.232 -18.584  1.00  0.00     
ATOM     10  BB  VAL 1   6     -47.498 -23.718 -18.979  1.00  0.00     

Then I have a second file that looks like that:

1
3
6

What I want to do is: In those lines of file1 where $6 has one of the values of file2, I add an additional column in file1 $12=="cg". The output should look like this:

ATOM      1  BB  SER 1   1     -31.958 -25.125 -11.061  1.00  0.00  cg   
ATOM      3  BB  GLY 1   2     -32.079 -26.085 -14.466  1.00  0.00      
ATOM      4  BB  VAL 1   3     -36.455 -21.265 -15.792  1.00  0.00  cg    
ATOM      6  BB  SER 1   4     -37.401 -20.877 -19.029  1.00  0.00     
ATOM      8  BB  ALA 1   5     -42.701 -21.232 -18.584  1.00  0.00     
ATOM     10  BB  VAL 1   6     -47.498 -23.718 -18.979  1.00  0.00  cg

Could anyone help me please? That would be great! Normally I am using awk or perl :slight_smile:

Thank you so much,
Christine

 awk 'FNR == NR { A[$1] = $1; next } {print A[$6]?$0 " cg": $0}' file1 file2

ATOM 1 BB SER 1 1 -31.958 -25.125 -11.061 1.00 0.00 cg
ATOM 3 BB GLY 1 2 -32.079 -26.085 -14.466 1.00 0.00
ATOM 4 BB VAL 1 3 -36.455 -21.265 -15.792 1.00 0.00 cg
ATOM 6 BB SER 1 4 -37.401 -20.877 -19.029 1.00 0.00
ATOM 8 BB ALA 1 5 -42.701 -21.232 -18.584 1.00 0.00
ATOM 10 BB VAL 1 6 -47.498 -23.718 -18.979 1.00 0.00  cg

where file1 is the 1,3 6 file and file2 the other file.