I am trying to use sed to replace a path in a file.
sudo sed 's/a/b/g' -i /tmp/test
However the variable is
a = /var/lib and
b = /data/lib
How do I escape the slash?
I am trying to use sed to replace a path in a file.
sudo sed 's/a/b/g' -i /tmp/test
However the variable is
a = /var/lib and
b = /data/lib
How do I escape the slash?
The character just after the s
command doesn't need to be a /
. When working with paths, I use :
, as in:
sudo sed 's:a:b:g' -i /tmp/test
You can change sed's delimiter for instance use #
instead:
$ sed 's#/var/lib#data/lib#g'
/var/lib
data/lib
this should work
sed -i "s@$a@$b@g" /tmp/test
two things you need to take care about:
1) if you want to use variables in your sed line, use double quotes
2) delimiter could be other than "/
", e.g. @, #, : ...
In sed(1), as in vi(1), the '/' is just the customary separator. It can be escaped with \
, leading to "leaning toothpick syndrome" when munging path names:
sed -e 's/\/var\/lib/data\/lib/' ...
You can use another non-word character, e.g. ';':
sed -e 's;/var/lib;data/lib; ...