【发布时间】:2012-05-05 19:14:05
【问题描述】:
我无法弄清楚如何使用 sed 在文本文件 /etc/myconfig 中搜索和替换包含 / 字符的字符串。
例如,在我现有的文本文件中,我有:
myparam /path/to/a argB=/path/to/B xo
我希望将其替换为:
myparam /path/to/c argB=/path/to/D xo
我尝试在 bash 中这样做:
line='myparam /path/to/a argB=/path/to/B xo'
line_new='myparam /path/to/c argB=/path/to/D xo'
sed -i 's/$line/$line_new/g' /etc/myconfig
但是什么也没发生。
尝试
grep -rn "$line" /etc/myconfig
确实返回给我'myparam /path/to/a argB=/path/to/B xo'。
表达我的sed 命令以执行此搜索和替换并正确处理/ 命令的正确方法是什么? (我认为我的字符串中的/ 字符是给我带来问题的字符,因为我使用了类似的sed 命令来搜索和替换文本文件中的另一行而没有问题,并且该行没有@987654333 @ 字符。
【问题讨论】:
-
替换和引用规则实际上是关于 bash 而不是专门针对 sed。您可能希望将 bash 添加为标签。
-
好的。将
bash添加为标签。