You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改GROMACS拓扑文件[atoms]段的混合字符串数字列?

处理GROMACS拓扑文件中[atoms]段的原子名称修改

问题描述

有一个制表符分隔的GROMACS拓扑文件,需要仅修改[ atoms ]段的第五列(atom列),移除该列中的数字部分,其他段落内容保持不变。

原始文件内容

[ moleculetype ]
; Name nrexcl
AL7 3

[ atoms ]
; nr type resnr resid atom cgnr charge mass
   1 CB   1  AL6 C4      1 -0.1435  12.0110
   2 CB   1  AL6 C5      2 -0.1500  12.0110
   3 CB   1  AL6 C6      3 -0.1500  12.0110
   4 CB   1  AL6 C7      4  0.0825  12.0110
   5 CB   1  AL6 O8      5 -0.1500  12.0110

[ bonds ]
; ai aj fu b0 kb, b0 kb
 16   7 1 0.10930  287014.9  0.10930  287014.9
 15   7 1 0.10930  287014.9  0.10930  287014.9
  7   8 1 0.14180  303937.5  0.14180  303937.5
  7  17 1 0.10930  287014.9  0.10930  287014.9
  8   9 1 0.13550  349343.9  0.13550  349343.9
 20  12 1 0.10190  390836.6  0.10190  390836.6

期望输出

[ moleculetype ]
; Name nrexcl
AL7 3

[ atoms ]
; nr type resnr resid atom cgnr charge mass
   1 CB   1  AL6 C      1 -0.1435  12.0110
   2 CB   1  AL6 C      2 -0.1500  12.0110
   3 CB   1  AL6 C      3 -0.1500  12.0110
   4 CB   1  AL6 C      4  0.0825  12.0110
   5 CB   1  AL6 O      5 -0.1500  12.0110

[ bonds ]
; ai aj fu b0 kb, b0 kb
 16   7 1 0.10930  287014.9  0.10930  287014.9
 15   7 1 0.10930  287014.9  0.10930  287014.9
  7   8 1 0.14180  303937.5  0.14180  303937.5
  7  17 1 0.10930  287014.9  0.10930  287014.9
  8   9 1 0.13550  349343.9  0.13550  349343.9
 20  12 1 0.10190  390836.6  0.10190  390836.6

解决方案

使用awk的状态机逻辑,仅在[ atoms ]段内处理目标列,同时维持文件格式对齐:

awk '
/^\[ atoms \]/ { in_atoms = 1 }
/^\[ / && in_atoms { in_atoms = 0 }
in_atoms && !/^;/ && NF >= 5 {
    # 移除第五列末尾的所有数字
    sub(/[0-9]+$/, "", $5)
    # 格式化第五列,保持原列宽(示例中原列宽为2,按需调整)
    $5 = sprintf("%-2s", $5)
}
1' input.top > output.top

命令说明

  1. 状态标记:通过in_atoms变量标记是否处于[ atoms ]段内,遇到[ atoms ]时开启标记,遇到其他以[ 开头的段落时关闭标记
  2. 目标行处理:仅在in_atoms为1、非注释行、列数足够时,对第五列执行替换操作,移除末尾的数字
  3. 格式对齐:用sprintf("%-2s", $5)保证处理后的第五列宽度与原文件一致,避免格式混乱
  4. 输出所有行:最后一行的1表示打印所有行,确保非目标段内容完全保留

内容的提问来源于stack exchange,提问作者subhadip das

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 18:47:20