I need a sed
command to replace a line in config file that contains /
.
I want to change /var/log/haproxy
to /root/myDir/haproxy
in the file.
Can you please help? Thanks in advance.
I need a sed
command to replace a line in config file that contains /
.
I want to change /var/log/haproxy
to /root/myDir/haproxy
in the file.
Can you please help? Thanks in advance.
If you want to replace a string containing slashes, just use a different delimiter for the sed
expression:
sed 's@/var/log/haproxy@/root/myDir/haproxy@g' <file>
Here's an example:
$ echo /var/log/haproxy | sed 's@/var/log/haproxy@/root/myDir/haproxy@g'
/root/myDir/haproxy
sed
will automatically use whatever character follows the s
as the delimiter. These will also work:
$ echo /var/log/haproxy | sed 's#/var/log/haproxy#/root/myDir/haproxy#g'
/root/myDir/haproxy
$ echo /var/log/haproxy | sed 's^/var/log/haproxy^/root/myDir/haproxy^g'
/root/myDir/haproxy
$ echo /var/log/haproxy | sed 's%/var/log/haproxy%/root/myDir/haproxy%g'
/root/myDir/haproxy
You can also just escape the /
's in the path and keep using /
, but that's harder to read, so I don't recommend it:
$ echo /var/log/haproxy | sed 's/\/var\/log\/haproxy/\/root\/myDir\/haproxy/g'
/root/myDir/haproxy