忽略sed find and replace命令中的斜杠 [英] Ignoring slashes in sed find and replace command
问题描述
我正在尝试在终端中使用sed进行查找和替换.
I'm trying to do a find and replace using sed in a terminal.
在apache配置文件中,我正在尝试替换:
Within the apache config file, I'm trying to replace:
DocumentRoot/var/www
使用
DocumentRoot/var/www/mysite.com/public_html
从命令行开始,无需使用编辑器.我想使用sed进行此操作,并且尝试了以下各种迭代:
From the command line, without using an editor. I'd like to do this with sed, and I've tried various iterations of:
sudo sed -i's/'DocumentRoot/var/www'/'DocumentRoot/var/www/mysite.com/public_html'/'/etc/apache2/sites-available/mysite.com代码>
但是,当我这样做时会出现此错误: sed:-e表达式#1,字符14:未终止的s命令
However when doing so I get this error:
sed: -e expression #1, char 14: unterminated s command
因此,路径中的斜杠上出现错误.我该如何解决?
So it's erroring on the slashes in the paths. How do I get around this?
感谢您的帮助
推荐答案
没有必要使用/
作为正则表达式分隔符.当您的正则表达式包含本来需要转义的/
时,可以使用#
或 @
.
It isn't necessary to use /
as the regex delimiter. You can use #
or @
when your regular expressions contain /
that you would otherwise need to escape.
sed -i s#expr1#expr2#
因此,可能是:
sudo sed -i 's#DocumentRoot /var/www#DocumentRoot /var/www/mysite.com/public_html#' /etc/apache2/sites-available/mysite.com
此外,由于您的替换参数本身包含空格,因此您需要将其用单引号引起来.
Also, as your substitution-argument itself contains spaces, you need to enclose them in single-quotes.
这篇关于忽略sed find and replace命令中的斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!