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

Maarten Litmaath maart at cs.vu.nl
Wed Jan 30 02:23:49 AEST 1991


In article <2586 at root44.co.uk>,
	gwc at root.co.uk (Geoff Clare) writes:
)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'

Did you actually try your solution?  I guess not.
The following works:

Xstring=`sed 's/[\/&\\\\]/\\\\&/g' <<!
$string
!
`
--
kinnersley at kuhub.cc.ukans.edu (Bill Kinnersley):
	"Do phonograph turntables turn the other way in Australia?"
gjh at krebs.acc.Virginia.EDU (Galen J. Hekhuis):
	"How do you think satanic messages were discovered on records?"



More information about the Comp.unix.shell mailing list