用列中的值替换增量值并保持格式不变

时间:2013-03-11 09:38:53

标签: perl

我有一个像这样的文件

TER 
ATOM  18351  C12 PA      1      22.484  26.043  12.870  1.00  0.00      MEMB
ATOM  18352  H2R PA      1      21.907  26.993  12.853  1.00  0.00      MEMB
ATOM  18353  H2S PA      1      22.369  25.522  11.895  1.00  0.00      MEMB
ATOM  18401  C13 PA      1      23.999  26.415  12.988  1.00  0.00      MEMB
ATOM  18402  H3R PA      1      24.586  25.473  12.934  1.00  0.00      MEMB
ATOM  18403  H3S PA      1      24.188  26.890  13.975  1.00  0.00      MEMB
ATOM  18404  C14 PA      1      24.389  27.402  11.877  1.00  0.00      MEMB
ATOM  18405  H4R PA      1      25.379  27.803  12.184  1.00  0.00      MEMB
ATOM  18406  H4S PA      1      23.777  28.329  11.879  1.00  0.00      MEMB
ATOM  18407  C15 PA      1      24.484  26.765  10.484  1.00  0.00      MEMB
TER
ATOM  18408  H5R PA      1      23.540  26.275  10.164  1.00  0.00      MEMB
ATOM  18409  H5S PA      1      25.223  25.936  10.474  1.00  0.00      MEMB
ATOM  18410  C16 PA      1      24.922  27.998   9.564  1.00  0.00      MEMB
ATOM  18411  H6R PA      1      25.694  28.592  10.099  1.00  0.00      MEMB
ATOM  18412  H6S PA      1      24.035  28.658   9.457  1.00  0.00      MEMB
ATOM  18413  C17 PA      1      25.447  27.503   8.237  1.00  0.00      MEMB
ATOM  18414  H7R PA      1      25.871  26.479   8.305  1.00  0.00      MEMB
ATOM  18415  H7S PA      1      26.266  28.169   7.891  1.00  0.00      MEMB

我想在第5列中的每个TER之后增加1,以便输出文件看起来像:

TER 
ATOM  18351  C12 PA      1      22.484  26.043  12.870  1.00  0.00      MEMB
ATOM  18352  H2R PA      1      21.907  26.993  12.853  1.00  0.00      MEMB
ATOM  18353  H2S PA      1      22.369  25.522  11.895  1.00  0.00      MEMB
ATOM  18401  C13 PA      1      23.999  26.415  12.988  1.00  0.00      MEMB
ATOM  18402  H3R PA      1      24.586  25.473  12.934  1.00  0.00      MEMB
ATOM  18403  H3S PA      1      24.188  26.890  13.975  1.00  0.00      MEMB
ATOM  18404  C14 PA      1      24.389  27.402  11.877  1.00  0.00      MEMB
ATOM  18405  H4R PA      1      25.379  27.803  12.184  1.00  0.00      MEMB
ATOM  18406  H4S PA      1      23.777  28.329  11.879  1.00  0.00      MEMB
ATOM  18407  C15 PA      1      24.484  26.765  10.484  1.00  0.00      MEMB
TER
ATOM  18408  H5R PA      2      23.540  26.275  10.164  1.00  0.00      MEMB
ATOM  18409  H5S PA      2      25.223  25.936  10.474  1.00  0.00      MEMB
ATOM  18410  C16 PA      2      24.922  27.998   9.564  1.00  0.00      MEMB
ATOM  18411  H6R PA      2      25.694  28.592  10.099  1.00  0.00      MEMB
ATOM  18412  H6S PA      2      24.035  28.658   9.457  1.00  0.00      MEMB

此外,不应更改文件格式。

2 个答案:

答案 0 :(得分:0)

perl的:

perl -lane 'if(/TER/){$count++}unless(/TER/){$F[4]=$count;}print "@F"' your_file

AWK:

awk '/TER/{count++}{$5=count}1' your_file

答案 1 :(得分:0)

在不更改格式的情况下执行此操作的脏方法,如果第5列是唯一一个在数据中包含整数1的方法:

perl -pe '$c++if/TER/;s/\b1\b/$c/' input_file