sed 搜索并替换包含/的字符串 [英] sed search and replace strings containing /

查看:56
本文介绍了sed 搜索并替换包含/的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何使用 sed 来搜索和替换文本文件 /etc/myconfig/ 字符的字符串>.

I am having trouble figuring out how to use sed to search and replace strings containing the / character in a text file /etc/myconfig.

例如,在我现有的文本文件中,我有:

For instance, in my existing text file, I have:

myparam /path/to/a argB=/path/to/B xo

并且我希望将其替换为:

and I want this replaced by:

myparam /path/to/c argB=/path/to/D xo

我尝试在 bash 中执行此操作:

I attempted doing this in 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' 不过.

does return me 'myparam /path/to/a argB=/path/to/B xo' though.

表达我的 sed 命令以执行此搜索并替换和正确处理 / 命令的正确方法是什么?(我认为字符串中的 / 字符是给我带来问题的字符,因为我使用了类似的 sed 命令来搜索和替换文本文件中的另一行,而没有问题并且该行没有 / 字符.

What's the correct way to express my sed command to execute this search and replace and correctly deal with the / command? (I reckon that the / character in my strings are the ones giving me the problem because I used a similar sed command to search and replace another line in the text file with no problems and that line does not have a / character.

推荐答案

不要转义反斜杠;你会迷惑自己.在文本中没有出现的 s 命令之后使用不同的符号(我在下面的示例中使用了 %):

Don't escape the backslashes; you'll confuse yourself. Use a different symbol after the s command that doesn't appear in the text (I'm using % in the example below):

line_old='myparam /path/to/a argB=/path/to/B xo'
line_new='myparam /path/to/c argB=/path/to/D xo'
sed -i "s%$line_old%$line_new%g" /etc/myconfig

另外,用双引号将整个字符串括起来;使用单引号意味着 sed 看到的是 $line (在原始中)而不是扩展值.在单引号内,没有扩展,也没有元字符.如果您的文本几乎可以包含任何纯文本字符,请使用控制字符(例如 control-A 或 control-G)作为分隔符.

Also, enclose the whole string in double quotes; using single quotes means that sed sees $line (in the original) instead of the expanded value. Inside single quotes, there is no expansion and there are no metacharacters. If your text can contain almost any plain text character, use a control character (e.g. control-A or control-G) as the delimiter.

请注意,此处使用 -i 反映了问题中的内容,但前提是使用了 GNU sed.BSD sed(Mac OS X 上也有)需要一个后缀.可以使用 sed -i '' ... 原位替换;这不适用于 GNU sed.要在两者之间移植,请使用 -i.bak;这对两者都适用——但会给你一个你可能想要删除的备份文件.其他 Unix 平台(例如 AIX、HP-UX、Solaris)可能具有根本不支持 -ised 变体.sed.

Note that the use of -i here mirrors what is in the question, but that assumes the use of GNU sed. BSD sed (found on Mac OS X too) requires a suffix. You can use sed -i '' … to replace in situ; that does not work with GNU sed. To be portable between the two, use -i.bak; that will work with both — but gives you a backup file that you'll probably want to delete. Other Unix platforms (e.g. AIX, HP-UX, Solaris) may have variants of sed that do not support -i at all. It is not required by the POSIX specification for sed.

这篇关于sed 搜索并替换包含/的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆