简体   繁体   English

在外壳程序脚本中使用sed用反斜杠替换一行

[英]using sed to replace a line with back slashes in a shell script

I am trying to replace the bottom one of these 2 lines with sed in a file. 我试图用文件中的sed替换这2行的底部。

    <rule>out_prefix=orderid ^1\\d\+ updatemtnotif/</rule>\n\
    <rule>out_prefix=orderid ^2\\d\+ updatemtnotif/</rule>\n\

And the following command seems to do that when executed as a command at the bash prompt 当在bash提示符下作为命令执行时,以下命令似乎可以执行此操作

sed -i 's@out_prefix=orderid ^2\\\\d\\+ updatemtnotif/@out_prefix=orderid ^2\\\\d\\+ updatemtnotif_fr/@g' /opt/temp/rules.txt

however, when I try to execute the same command remotely over ssh using here documents, the command fails to modify the file. 但是,当我尝试使用here文档通过ssh远程执行同一命令时,该命令无法修改文件。 I think this is probably an escaping issue, but I have had no luck trying to modify the command in numerous ways. 我认为这可能是一个不可避免的问题,但是我尝试以多种方式修改命令并不幸运。 Can any one tell me what should I do to get it working over ssh? 谁能告诉我该如何通过ssh进行工作? Thanks in advance! 提前致谢!

to clarify, 澄清,

input: <rule>out_prefix=orderid ^2\\d\+ updatemtnotif/</rule>\n\
output: <rule>out_prefix=orderid ^2\\d\+ updatemtnotif_fr/</rule>\n\

You can use it with ssh and heredoc like this: 您可以将它与ssh和heredoc一起使用,如下所示:

ssh -t -t user@localhost<<'EOF'
sed 's~out_prefix=orderid ^2\\\\d\\+ updatemtnotif/~out_prefix=orderid ^2\\\\d\\+ updatemtnotif_fr/~' ~/path/to/file
exit
EOF

PS: It is important to quote the 'EOF' as shown. PS:重要的是引用如图所示的'EOF'

I managed to fix it. 我设法解决了。 had to escape the backslashes in the command I used inside the shell script. 必须在shell脚本中使用的命令中转义反斜杠。

 's@out_prefix=orderid ^2\\\\\\\\d\\\\+ updatemtnotif/@out_prefix=orderid ^2\\\\\\\\d\\\\+ updatemtnotif_fr/@g' /opt/temp/rules.txt

That's a whole lot of backslashes but it did the trick. 这是很多反斜杠,但确实成功了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM