-1

I have a file (.pdb) that looks like this:

ATOM      1  BB  MET A   1       4.171  16.195 -18.221  1.00  0.00           B 
ATOM      2  SC1 MET A   1       0.852  15.586 -20.418  1.00  0.00           S 
ATOM      3  BB  GLU A   3       9.285  12.756 -18.753  1.00 22.00           B 

I would like to replace the values in column 11 by another one from another txt file that looks like this:

4.61
4.80
15.81

The output should be:

ATOM      1  BB  MET A   1       4.171  16.195 -18.221  1.00  4.61           B 
ATOM      2  SC1 MET A   1       0.852  15.586 -20.418  1.00  4.80           S
ATOM      3  BB  GLU A   3       9.285  12.756 -18.753  1.00 15.81           B 

I tried with awk the following

awk ' NR==FNR{a[NR]=$0; next}{$11=a[FNR]}1' file2.txt fil1.pdb > output.pdb

but the format is not preserved. I got something like this:

ATOM 1 BB MET A 1 4.171 16.195 -18.221 1.00 4.61 B
ATOM 2 SC1 MET A 1 0.852 15.586 -20.418 1.00 4.80 S
ATOM 3 SC1 GLU A 3 9.285 12.756 -18.753 1.00 15.81 B

Any suggestions to retain the format while substitution?

pii_ke
  • 2,811
  • 2
  • 20
  • 30
Zeineb
  • 59
  • 1
  • 1
  • 7
  • Please post sample file data in text format in question – anubhava Aug 13 '20 at 17:23
  • The pdb format is a fixed-width format. In your example you replace B-factor, which is in columns 61-66 (column = position in the line). – marcin Aug 17 '20 at 16:09

1 Answers1

0

Here a an awk that should work on non-gnu versions also:

awk ' NR == FNR {
   a[NR] = $0
   next
}
match($0, /^[[:blank:]]*([^[:blank:]]+[[:blank:]]+){10}/) {
   s = substr($0, RLENGTH+1)
   sub(/[^[:blank:]]+/, "", s)
   print substr($0, 1, RLENGTH) a[FNR] s
}' file2.txt fil1.pdb
ATOM      1  BB  MET A   1       4.171  16.195 -18.221  1.00  4.61           B
ATOM      2  SC1 MET A   1       0.852  15.586 -20.418  1.00  4.80           S
ATOM      3  BB  GLU A   3       9.285  12.756 -18.753  1.00 15.81           B
anubhava
  • 761,203
  • 64
  • 569
  • 643