替换列并保持相同格式

时间:2018-07-23 16:15:33

标签: bash awk

我有一个看起来像这样的文件

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            

我想用0.00代替第9列,并且像下面这样使用awk

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

但这完全弄乱了格式。有什么建议在替换时保留格式吗?

这与column replacement with awk, with retaining the format类似,但是我没有得到答案中提到的split()参数。

3 个答案:

答案 0 :(得分:2)

您可以将awk命令传送到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

答案 1 :(得分:1)

使用sed可以更容易地替换倒数第二个字段

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

如果您的[^\s]支持,可以将\S替换为sed

答案 2 :(得分:1)

使用具有TypeError: Cannot read property 'then' of undefined at new AdminUserDetailsController (bundle.js:38254) at Object.instantiate (bundle.js:6395) at $controller (bundle.js:12447) at Object.link (bundle.js:1247) at bundle.js:2636 at invokeLinkFn (bundle.js:11994) at nodeLinkFn (bundle.js:11371) at compositeLinkFn (bundle.js:10642) at publicLinkFn (bundle.js:10507) at lazyCompilation (bundle.js:10898) "<div ng-view="" class="ng-scope">" 用于ERE的arg的sed(例如GNU sed)设置第9个字段的一般方法:

-E

使用GNU awk将第四个arg设置为split()的第9个字段的通用方法:

$ 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