通过识别 linux 中的整数或实数在文件中的特定位置输入文本

entering text in a file at specific locations by identifying the number being integer or real in linux

我有如下输入

    46742       1   48276   48343   48199   48198
    46744       1   48343   48344   48200   48199
    46746       1   48344   48332   48201   48200
    48283  3.58077402e+01 -2.97697746e+00  1.50878647e+02
    48282  3.67231688e+01 -2.97771595e+00  1.50419488e+02
    48285  3.58558188e+01 -1.98122787e+00  1.50894850e+02

第二个条目如 1 为整数的每个段就像数千行,然后以第二个条目为实数开始该段,如 3.58077402e+01

在任何存在之前我必须输入像

这样的文本
*Revolved
*Gripped
*Crippled
    46742       1   48276   48343   48199   48198
    46744       1   48343   48344   48200   48199
    46746       1   48344   48332   48201   48200
*Cracked
*Crippled
    48283  3.58077402e+01 -2.97697746e+00  1.50878647e+02
    48282  3.67231688e+01 -2.97771595e+00  1.50419488e+02
    48285  3.58558188e+01 -1.98122787e+00  1.50894850e+02

所以我需要在这些位置输入特定的文本。值得一提的是,该文件是 space 分隔的,而不是制表符分隔的,以 * 开头的文本必须位于该行的最左侧,没有空格。文件其余部分的格式也应保留。

如有任何关于 sed 或 awk 的建议,我们将不胜感激! 开头的文字可以直接输入,所以不是主要问题,因为那是文件的开头,有问题的是第二行,因此确定第二个条目已变为真实。

具有固定字符串的 awk

awk 'BEGIN{print "*Revolved\n*Gripped\n*Crippled"}
     match(,"\+")&&!pr{print "*Cracked\n*Crippled";pr=1}1' yourfile

match(,"\+")&&!pr :当在 </code> 字段(实数)找到 <code>+ 字符且 pr 标志为 null.