sed 's/foobar/$string/g'.... can't do this?

Geoff Clare gwc at root.co.uk
Tue Jan 29 00:27:37 AEST 1991


In <1064 at mwtech.UUCP> martin at mwtech.UUCP (Martin Weitzel) writes:

>	sed 's/foobar/'"$string"'/g'

>BTW: There's no simple way to stop sed from complaining when the variable
>`string' contains a slash, except if you know that it does contain a slash
>and use some other seperator in the s-commando.

Yes there is.  Simply create a copy of $string with '/' changed to '\/'.
While you're at it, you can treat '&' and '\' the same way to prevent
sed from interpreting '&', '\1', etc. if they happen to occur in $string.

Xstring=`sed 's/[\/&\\]/\\&/g' <<!
$string
!
`
sed 's/foobar/'"$Xstring"'/g'


-- 
Geoff Clare <gwc at root.co.uk>  (Dumb American mailers: ...!uunet!root.co.uk!gwc)
UniSoft Limited, London, England.   Tel: +44 71 729 3773   Fax: +44 71 729 3273



More information about the Comp.unix.shell mailing list