find and replace with sed with slash in find and replace string

Not sure if you know, but sed has a great feaure were you do not need to use a / as the separator.

So, your example could be written as:

sed -i 's#/var/www#/home/lokesh/www#g' lks.php

It does not need to be a # either, it could be any single character. For example, using a 3 as the separator:

echo "foo" | sed 's3foo3bar3g'
bar

You can use a different character for the delimiter, as others have pointed out, like this:

sed -i 's!pattern!replacement!g' /path/to/file

But if there is no convenient character to use instead of / as the delimiter, you can also escape any embedded delimiter characters in your pattern and replacement by prefixing them each with a backslash \. Here's an example using / as the delimiter that replaces /some/path with ../some/replacement:

sed -i 's/\/some\/path/..\/some\/replacement/g'

It's harder for humans to read, though, so generally if it's at all possible it's better to use a different character for the delimiter like this:

sed -i 's!/some/path!../some/replacement!g'

Have you try to use something like:

sed -i 's@/var/www@/home/lokesh/www@g' lks.php

Tags:

Sed

Quoting