How to replace a path with another path in sed?

PathSedCsh

Path Problem Overview


I have a csh script (although I can change languages if it has any relevance) where I have to:

sed s/AAA/BBB/ file

The problem is that AAA and BBB are paths, and so contain '/'. AAA is fixed, so I can say:

sed s/\\\/A\\\/A\\\A/BBB/ file

However, BBB is based on variables, including $PWD. How do I escape the '/' in $PWD?

OR is there some other way I should be doing this entirely?

Path Solutions


Solution 1 - Path

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.

Solution 2 - Path

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

Solution 3 - Path

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

EDIT 1

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

Solution 4 - Path

Using csh for serious scripting is usually not recommended. However, that is tangential to the issue at hand.

You're probably after something like:

sed -e "s=$oldpath=$newpath="

where the shell variable $oldpath contains the value to be replaced and $newpath contains the replacement, and it is assumed that neither variable contains an equals sign. That is, you're allowed to choose the delimiter on pattern, and avoiding the usual / delimiter avoids problems with slashes in pathnames. If you think = might appear in your file names, choose something less likely to appear, such as control-A or control-G.

Solution 5 - Path

In my case the below method works.

sed -i 's/playstation/PS4/' input.txt

Can be also be written as

sed -i 's+playstation+PS4+' input.txt
  • sed : is stream editor

  • -i : Allows to edit the source file

  • +: Is delimiter.

I hope the above information works for you .

Solution 6 - Path

We just needed to get the /h/ network path references out of the path. if we pointed them back to the /c/ drive they would map to non-existant directories but resolve quickly. In my .bashrc I used

PATH=`echo $PATH | sed -e "s+/h/+/c/+g"`

Solution 7 - Path

You can use parenthesis expansion ${i/p/r} to escape the slashes. In this case ${i//p/r} for escaping all occurrences.

$p1=${p1//\//\\/}
$p2=${p2//\//\\/}
sed s/$p1/$p2/ file

Or, more concise, in one line sed s/${p1//\//\\/}/${p2//\//\\/}/ file

The two fist slashes // are a separator in parenthesis expansion saying we are matching all occurrences, then \/ is for escaping the slash in the search template, the / as a second separator in the expansion, and then \\/ is the replacement, in witch the backslash must be escaped.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian PostowView Question on Stackoverflow
Solution 1 - PathLev LevitskyView Answer on Stackoverflow
Solution 2 - Pathc0m3tView Answer on Stackoverflow
Solution 3 - PathpyloverView Answer on Stackoverflow
Solution 4 - PathJonathan LefflerView Answer on Stackoverflow
Solution 5 - Pathvijayraj34View Answer on Stackoverflow
Solution 6 - PathD M LoweView Answer on Stackoverflow
Solution 7 - PathFrViPofmView Answer on Stackoverflow