sed – using variables containing "/"

Avi Greenbury lists at avi.co
Sun Feb 12 19:22:34 UTC 2012


Johnny Rosenberg wrote:
> So, if the variable MyVar contains a ”/”, sed will fail and a
> workaround is to use another character instead of the ”/” character
> for sed, like in this case, ”@”.

Yeah, rather than s/4\/3/four-thirds/ one might do s at 4/3 at four-thirds@

> Is this the only workaround in this case or is there a more elegant
> one? The variable ”MyVar” must not be changed in any way, other than
> that, everything is allowed, I suppose…

If you can't escape it, then yeah that's all you can do in sed as far
as I'm aware. That doesn't strike me as particularly inelegant
(especially considering how much I use sed for changing filepaths) - is
there some other reason to not do it that way?

If you want to be more flexible, though, and so permit all characters
in the strings (or at least not need to predict what might be in them)
it would probably be easiest to pipe it into a perl one-liner.

-- 
Avi.




More information about the ubuntu-users mailing list