如何将包含斜杠的变量传递给 sed [英] How to pass a variable containing slashes to sed
本文介绍了如何将包含斜杠的变量传递给 sed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将包含斜杠的变量作为模式传递给 sed
?
How do you pass a variable containing slashes as a pattern to sed
?
例如,如果我有以下变量:
For example, if I have the following variable:
var="/Users/Documents/name/file"
我想将它传递给 sed
如下:
I want to pass it to sed
as so:
sed "s/$var/replace/g" "$file"
但是我遇到了错误.我怎样才能规避这个问题?
However I get errors. How can I circumvent the issue?
推荐答案
使用备用正则表达式分隔符作为 sed
允许您使用任何分隔符(包括控制字符):
Use an alternate regex delimiter as sed
allows you to use any delimiter (including control characters):
sed "s~$var~replace~g" $file
如上所述,我们也可以使用控制字符作为分隔符:
As mentioned above we can use control character as delimiter as well like:
sed "s^[$var^[replace^[g" file
其中 ^[
是通过同时按下 Ctrl-V-3 输入的.
Where ^[
is typed by pressing Ctrl-V-3 together.
这篇关于如何将包含斜杠的变量传递给 sed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文