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
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
But nothing happens.
Attempting
grep -rn "$line" /etc/myconfig
does return me 'myparam /path/to/a argB=/path/to/B xo' though.
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.
Don’t escape the backslashes; you’ll confuse yourself. Use a different symbol after the
scommand that doesn’t appear in the text (I’m using%in the example below):Also, enclose the whole string in double quotes; using single quotes means that
sedsees$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.Note that the use of
-ihere mirrors what is in the question, but that assumes the use of GNUsed. BSDsed(found on Mac OS X too) requires a suffix. You can usesed -i '' …to replace in situ; that does not work with GNUsed. 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 ofsedthat do not support-iat all. It is not required by the POSIX specification forsed.