1

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.

Will
  • 24,082
  • 14
  • 97
  • 108
DevOpsNewB
  • 185
  • 2
  • 14
  • sorry i did not understand what you meant. – DevOpsNewB Jul 12 '16 at 19:45
  • If you just want to replace `/var/log/haproxy` with `/root/myDir/haproxy`, do this: `sed 's@/var/log/haproxy@/root/myDir/haproxy@g'` – Will Jul 12 '16 at 19:45
  • 1
    Possible duplicate of [Use slashes in sed replace](http://stackoverflow.com/questions/5864146/use-slashes-in-sed-replace) – Will Jul 12 '16 at 19:52

1 Answers1

0

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
Will
  • 24,082
  • 14
  • 97
  • 108