Linux CentOS使用sed插入一行又一行文本无效
我试图插入一行文本,这是Linux CentOS使用sed插入一行又一行文本无效,linux,sed,centos,Linux,Sed,Centos,我试图插入一行文本,这是 include /etc/nginx/sites-enabled/*; 在单词后的下一行 for more information. 在my/etc/nginx/nginx.conf中,使用以下命令 sudo sed '/for more information.'/a include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf 但每次我运行它,它都会返回 > 我对这类事情不是很在行,所以我希望有人
include /etc/nginx/sites-enabled/*;
在单词后的下一行
for more information.
在my/etc/nginx/nginx.conf中,使用以下命令
sudo sed '/for more information.'/a include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf
但每次我运行它,它都会返回
>
我对这类事情不是很在行,所以我希望有人能帮我
干杯,删除第二个单引号。这是多余的:
sudo sed '/for more information./a include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf
另外,由于
匹配任何字符,并且您可能希望它只匹配一个句点,因此请按如下方式对其进行转义:
sudo sed '/for more information\./a include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf
或者这个:
sudo sed '/for more information[.]/a include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf
删除第二个单引号。这是多余的:
sudo sed '/for more information./a include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf
另外,由于
匹配任何字符,并且您可能希望它只匹配一个句点,因此请按如下方式对其进行转义:
sudo sed '/for more information\./a include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf
或者这个:
sudo sed '/for more information[.]/a include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf
您的
sed
命令中有一个虚假的'
。相反,您需要:
sudo sed -i '/for more information./a include /etc/nginx/nginx.conf' \
/etc/nginx/nginx.conf
(请参见约翰的回答,因为如果有可能出现带有的行,则应转义
,例如“以获取更多信息。请在关闭
后用信息填充”
)
您还应该添加
-i
选项,以在位编辑/etc/nginx/nginx.conf
。您的sed
命令中有一个伪'
。相反,您需要:
sudo sed -i '/for more information./a include /etc/nginx/nginx.conf' \
/etc/nginx/nginx.conf
(请参见约翰的回答,因为如果有可能出现带有的行,则应转义
,例如“以获取更多信息。请在关闭
后用信息填充”
)
您还应该添加
-i
选项以就地编辑/etc/nginx/nginx.conf
。我使用了您列出的最后一个命令,它只是打印了文件,但它实际上没有将行保存到文件中。@DavidCRankin指出,如果您想就地编辑,请添加-i
选项。如果要保存备份,请使用<代码> -I.BAK < /代码>,备份副本将保存在具有<代码> .bAK/COD>扩展的文件中。或者,如果您想使用实际的文件编辑器而不是流编辑器,请考虑使用<代码> ED>代码>而不是<代码> SED。我使用了您列出的最后一个命令,并打印出了该文件,但它实际上并没有将该行保存到文件中,正如@DavidCRankin指出的那样,如果您想就地编辑,请添加-i
选项。如果要保存备份,请使用<代码> -I.BAK < /代码>,备份副本将保存在具有<代码> .bAK/COD>扩展的文件中。或者,如果您想使用实际的文件编辑器而不是流编辑器,请考虑使用<代码> ED>代码>而不是<代码> SED。为了满足<代码> -i < /C> >:+1,谢谢。就连这只瞎了眼的老松鼠也时不时地会找到一颗坚果来满足对-i
:+1的需求。嘿,谢谢。即使是这只瞎眼的老松鼠也会时不时地找到一颗坚果。:)