Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何在某一行上计算变量的输出?_Linux_Bash_Awk_Sed - Fatal编程技术网

Linux 如何在某一行上计算变量的输出?

Linux 如何在某一行上计算变量的输出?,linux,bash,awk,sed,Linux,Bash,Awk,Sed,我需要修改包含此字符串的行上方的文件: [分子类型] 我尝试执行的插入保存在如下变量中: ATOMTYPES=$(sed -n '/^\[\ atomtypes\ \]/,/^\[\ moleculetype\ \]/p;/^\[\ moleculetype\ \]/q' LIG_GMX.top | sed '$d') 其中包含以下文本: [ atomtypes ] ;name bond_type mass charge ptype sigma ep

我需要修改包含此字符串的行上方的文件: [分子类型]

我尝试执行的插入保存在如下变量中:

ATOMTYPES=$(sed -n '/^\[\ atomtypes\ \]/,/^\[\ moleculetype\ \]/p;/^\[\ moleculetype\ \]/q' LIG_GMX.top | sed '$d')
其中包含以下文本:

[ atomtypes ]
;name   bond_type     mass     charge   ptype   sigma         epsilon       Amb
 oh       oh          0.00000  0.00000   A     3.06647e-01   8.80314e-01 ; 1.72  0.2104
 nc       nc          0.00000  0.00000   A     3.25000e-01   7.11280e-01 ; 1.82  0.1700
 nd       nd          0.00000  0.00000   A     3.25000e-01   7.11280e-01 ; 1.82  0.1700
 na       na          0.00000  0.00000   A     3.25000e-01   7.11280e-01 ; 1.82  0.1700
 c3       c3          0.00000  0.00000   A     3.39967e-01   4.57730e-01 ; 1.91  0.1094
 ca       ca          0.00000  0.00000   A     3.39967e-01   3.59824e-01 ; 1.91  0.0860
 ho       ho          0.00000  0.00000   A     0.00000e+00   0.00000e+00 ; 0.00  0.0000
 h1       h1          0.00000  0.00000   A     2.47135e-01   6.56888e-02 ; 1.39  0.0157
 ha       ha          0.00000  0.00000   A     2.59964e-01   6.27600e-02 ; 1.46  0.0150
我想要的输出如下所示:

[ atomtypes ]
;name   bond_type     mass     charge   ptype   sigma         epsilon       Amb
 oh       oh          0.00000  0.00000   A     3.06647e-01   8.80314e-01 ; 1.72  0.2104
 nc       nc          0.00000  0.00000   A     3.25000e-01   7.11280e-01 ; 1.82  0.1700
 nd       nd          0.00000  0.00000   A     3.25000e-01   7.11280e-01 ; 1.82  0.1700
 na       na          0.00000  0.00000   A     3.25000e-01   7.11280e-01 ; 1.82  0.1700
 c3       c3          0.00000  0.00000   A     3.39967e-01   4.57730e-01 ; 1.91  0.1094
 ca       ca          0.00000  0.00000   A     3.39967e-01   3.59824e-01 ; 1.91  0.0860
 ho       ho          0.00000  0.00000   A     0.00000e+00   0.00000e+00 ; 0.00  0.0000
 h1       h1          0.00000  0.00000   A     2.47135e-01   6.56888e-02 ; 1.39  0.0157
 ha       ha          0.00000  0.00000   A     2.59964e-01   6.27600e-02 ; 1.46  0.0150

[ moleculetype ]
但是,当我使用这些命令时:

sed "/^\[\ moleculetype\ \]/i | echo  $ATOMTYPES|" topol.top

出现以下错误:

sed: -e expression #1, char 50: extra characters after command

你走错了路。sed用于对单个字符串执行s/old/new操作,仅此而已。对于任何更有趣的东西,您应该使用awk。这可能是您希望未测试的内容,因为您没有提供我们可以测试的样本输入/输出:

awk '
NR==FNR {
    if ( /\[ atomtypes ]/ )    { f=1 }
    if ( /\[ moleculetype ]/ ) { f=0 }
    if ( f ) {
        atomtypes = atomtypes ors $0
        ors = ORS
    }
    next
}
/\[ moleculetype ]/ { print atomtypes }
{ print }
' LIG_GMX.top topol.top

如果您使用的是GNU awk,您可以将f=0更改为nextfile以提高效率。

也许echo$ATOMTYPES | sed?您还没有清楚地说明要实现什么。是否要使用另一个sed命令将一个sed命令插入到文件中?我强烈怀疑您要求的不是您想要的。请发布与示例输入匹配的示例输出。用例子解释比发布非工作命令更清楚。如果你看,你可以看到它是i\text,这就是它应该如何工作的。此外,在sed中,命令以换行符结束,而不是以;结束;。这个被解析为sed中的另一个命令。真正的命令终止符是换行符。因此,如果$ATOMTYPES是多行字符串,则sed不是解决方案,或者您需要用ex替换换行符。\n,就像sed-z的/\n/\\n/'。嗨,Ed,谢谢您的帮助-我刚刚更新了问题。现在清楚了吗?是的Ed,它起作用了。非常感谢!
awk '
NR==FNR {
    if ( /\[ atomtypes ]/ )    { f=1 }
    if ( /\[ moleculetype ]/ ) { f=0 }
    if ( f ) {
        atomtypes = atomtypes ors $0
        ors = ORS
    }
    next
}
/\[ moleculetype ]/ { print atomtypes }
{ print }
' LIG_GMX.top topol.top