This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
问题出在斜杠上:你的变量包含它们,最终的命令将类似于 sed "s/string/path/to/something/g" ,包含太多斜杠。
由于 sed 可以将任何字符作为分隔符(无需声明新的分隔符),因此您可以尝试使用替换字符串中未出现的另一个字符:
请注意,这不是防弹的:如果替换字符串稍后包含
@
,它会因同样的原因而中断,并且任何反斜杠序列如\1
仍将根据sed
规则进行解释。使用|
作为分隔符也是一个不错的选择,因为它的可读性与/
类似。The problem is with slashes: your variable contains them and the final command will be something like
sed "s/string/path/to/something/g"
, containing way too many slashes.Since
sed
can take any char as delimiter (without having to declare the new delimiter), you can try using another one that doesn't appear in your replacement string:Note that this is not bullet proof: if the replacement string later contains
@
it will break for the same reason, and any backslash sequences like\1
will still be interpreted according tosed
rules. Using|
as a delimiter is also a nice option as it is similar in readability to/
.