Roy Banerjee
Roy Banerjee

Reputation: 61

Replacement of a column and keeping the same format

I have a file looks like this

ATOM      1  N   GLN     1      68.560  76.330  53.810  1.00  0.00            
ATOM      2  H1  GLN     1      68.030  75.660  54.340  1.00  0.00            
ATOM      3  H2  GLN     1      67.890  76.960  53.390  1.00  0.00            
ATOM      4  H3  GLN     1      69.090  76.880  54.480  1.00  0.00            
ATOM      5  CA  GLN     1      69.370  75.700  52.760  1.00  0.00            
ATOM      6  HA  GLN     1      70.400  76.070  52.820  1.00  0.00            
ATOM      7  CB  GLN     1      69.490  74.180  52.870  1.00  0.00            
ATOM      8  HB2 GLN     1      69.650  73.790  53.880  1.00  0.00            
ATOM      9  HB3 GLN     1      68.520  73.740  52.650  1.00  0.00            
ATOM     10  CG  GLN     1      70.560  73.570  51.970  1.00  0.00            

I want to substitute column 9 with 0.00 and I used awk like following

awk '{if($9 == "1.00") $9 = "0.00"; print}' test.pdb >mod_test.pdb

But that completely messes up the format. Any suggestions to retain the format while substitution?

This is kind of similar post of column replacement with awk, with retaining the format but I didn't get the split() argument mentioned in the answer.

Upvotes: 0

Views: 282

Answers (3)

anubhava
anubhava

Reputation: 785146

You may pipe your awk command to column -t:

awk '$9 == "1.00" {$9 = "0.00"} 1' file | column -t

ATOM  1   N    GLN  1  68.560  76.330  53.810  0.00  0.00
ATOM  2   H1   GLN  1  68.030  75.660  54.340  0.00  0.00
ATOM  3   H2   GLN  1  67.890  76.960  53.390  0.00  0.00
ATOM  4   H3   GLN  1  69.090  76.880  54.480  0.00  0.00
ATOM  5   CA   GLN  1  69.370  75.700  52.760  0.00  0.00
ATOM  6   HA   GLN  1  70.400  76.070  52.820  0.00  0.00
ATOM  7   CB   GLN  1  69.490  74.180  52.870  0.00  0.00
ATOM  8   HB2  GLN  1  69.650  73.790  53.880  0.00  0.00
ATOM  9   HB3  GLN  1  68.520  73.740  52.650  0.00  0.00
ATOM  10  CG   GLN  1  70.560  73.570  51.970  0.00  0.00

Upvotes: 2

Ed Morton
Ed Morton

Reputation: 203502

The generic way to set the 9th field using any sed that has a -E arg for EREs (e.g. GNU sed):

$ sed -E 's/(([^[:space:]]+[[:space:]]+){8})[^[:space:]]+/\10.00/' file
ATOM      1  N   GLN     1      68.560  76.330  53.810  0.00  0.00
ATOM      2  H1  GLN     1      68.030  75.660  54.340  0.00  0.00
ATOM      3  H2  GLN     1      67.890  76.960  53.390  0.00  0.00
ATOM      4  H3  GLN     1      69.090  76.880  54.480  0.00  0.00
ATOM      5  CA  GLN     1      69.370  75.700  52.760  0.00  0.00
ATOM      6  HA  GLN     1      70.400  76.070  52.820  0.00  0.00
ATOM      7  CB  GLN     1      69.490  74.180  52.870  0.00  0.00
ATOM      8  HB2 GLN     1      69.650  73.790  53.880  0.00  0.00
ATOM      9  HB3 GLN     1      68.520  73.740  52.650  0.00  0.00
ATOM     10  CG  GLN     1      70.560  73.570  51.970  0.00  0.00

The generic way to set the 9th field using GNU awk for the 4th arg to split():

$ cat tst.awk
{
    split($0,flds,FS,seps)
    flds[9] = "0.00"
    out = seps[0]
    for (i=1; i<=NF; i++) {
        out = out flds[i] seps[i]
    }
    print out
}

$ awk -f tst.awk file
ATOM      1  N   GLN     1      68.560  76.330  53.810  0.00  0.00
ATOM      2  H1  GLN     1      68.030  75.660  54.340  0.00  0.00
ATOM      3  H2  GLN     1      67.890  76.960  53.390  0.00  0.00
ATOM      4  H3  GLN     1      69.090  76.880  54.480  0.00  0.00
ATOM      5  CA  GLN     1      69.370  75.700  52.760  0.00  0.00
ATOM      6  HA  GLN     1      70.400  76.070  52.820  0.00  0.00
ATOM      7  CB  GLN     1      69.490  74.180  52.870  0.00  0.00
ATOM      8  HB2 GLN     1      69.650  73.790  53.880  0.00  0.00
ATOM      9  HB3 GLN     1      68.520  73.740  52.650  0.00  0.00
ATOM     10  CG  GLN     1      70.560  73.570  51.970  0.00  0.00

Upvotes: 1

karakfa
karakfa

Reputation: 67497

might be easier with sed, to replace the penultimate field

$ sed -E 's/(\s)1.00(\s[^\s]+)$/\10.00\2/' file

you can replace [^\s] with \S if your sed supports it.

Upvotes: 1

Related Questions