How to replace a path with another path in sed?

sed -i "s|$fileWithPath|HAHA|g" file

EDIT 1

sed -i 's|path/to/foo|path/to/bar|g' file

sed can use any separator instead of / in the s command. Just use something that is not encountered in your paths:

s+AAA+BBB+

and so on.

Alternatively (and if you don't want to guess), you can pre-process your path with sed to escape the slashes:

pwdesc=$(echo $PWD | sed 's_/_\\/_g')

and then do what you need with $pwdesc.


In circumstances where the replacement string or pattern string contain slashes, you can make use of the fact that GNU sed allows an alternative delimiter for the substitute command. Common choices for the delimiter are the pipe character | or the hash # - the best choice of delimiting character will often depend on the type of file being processed. In your case you can try

sed -i 's#/path/to/AAA#/path/to/BBB#g' your_file

Note: The g after last # is to change all occurrences in file if you want to change first ouccurence do not use g

Tags:

Csh

Path

Sed