This question already has an answer here:
这个问题已经有了答案:
- sed - unknown option to `s' 1 answer
- sed -未知选项为' s'的答案。
I'm trying to use sed to update a config file using a bash script. I have a similar sed command right above this one in the script that runs fine. I can't seem to figure out why this is breaking:
我尝试使用sed来使用bash脚本更新配置文件。在运行良好的脚本中,我有一个类似的sed命令。我似乎弄不明白为什么会这样:
sed -i.bak \
-e s/"socketPath:'https://localhost:9091'"/"socketPath:'/socket'"/g \
$WEB_CONF
Any ideas?
什么好主意吗?
3 个解决方案
#1
14
The quotes and double quotes are causing problems. You are using them in view of the slashes in the string. In sed you can use another delimiter, such as a #.
引号和双引号引起了问题。您使用它们是为了查看字符串中的斜线。在sed中,可以使用另一个分隔符,例如#。
sed -e 's#socketPath:https://localhost:9091#socketPath:/socket#g' \
$WEB_CONF
#2
4
Escape your slashes in the pattern or use a different delimiter like this:
在模式中跳出您的斜线,或者使用如下的分隔符:
sed -i.bak \
-e s%"socketPath:'https://localhost:9091'"%"socketPath:'/socket'"%g \
$WEB_CONF
#3
1
I am confused looking at the delimiters you have used so you can't blame sed
for goofing up. When your dataset has /
in them, it is often advised to use a different delimiters like #
or _
(yes, sed
supports various delimiters).
我对你使用过的分隔符感到困惑,所以你不能怪它搞砸了。当您的数据集/在它们中时,通常建议使用不同的分隔符,比如#或_(是的,sed支持各种分隔符)。
Also, your quoting looks abit off. The syntax should be:
而且,你的引文看起来也不一样。语法应该是:
sed -i.bak 's#substitute#replacement#g' "$WEB_CONF"
If your substitute and/or replacement has variables use "
instead '
which will allow it to interpolate.
如果你的替换和/或替换有变量使用“代替”,这将允许它插入。
#1
14
The quotes and double quotes are causing problems. You are using them in view of the slashes in the string. In sed you can use another delimiter, such as a #.
引号和双引号引起了问题。您使用它们是为了查看字符串中的斜线。在sed中,可以使用另一个分隔符,例如#。
sed -e 's#socketPath:https://localhost:9091#socketPath:/socket#g' \
$WEB_CONF
#2
4
Escape your slashes in the pattern or use a different delimiter like this:
在模式中跳出您的斜线,或者使用如下的分隔符:
sed -i.bak \
-e s%"socketPath:'https://localhost:9091'"%"socketPath:'/socket'"%g \
$WEB_CONF
#3
1
I am confused looking at the delimiters you have used so you can't blame sed
for goofing up. When your dataset has /
in them, it is often advised to use a different delimiters like #
or _
(yes, sed
supports various delimiters).
我对你使用过的分隔符感到困惑,所以你不能怪它搞砸了。当您的数据集/在它们中时,通常建议使用不同的分隔符,比如#或_(是的,sed支持各种分隔符)。
Also, your quoting looks abit off. The syntax should be:
而且,你的引文看起来也不一样。语法应该是:
sed -i.bak 's#substitute#replacement#g' "$WEB_CONF"
If your substitute and/or replacement has variables use "
instead '
which will allow it to interpolate.
如果你的替换和/或替换有变量使用“代替”,这将允许它插入。