Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!hp4nl!star.cs.vu.nl!maart From: maart@cs.vu.nl (Maarten Litmaath) Newsgroups: comp.unix.shell Subject: Re: sed 's/foobar/$string/g'.... can't do this? Message-ID: <8915@star.cs.vu.nl> Date: 1 Feb 91 20:26:21 GMT References: <1991Jan17.003856.469@unicorn.cc.wwu.edu> <135@edi386.UUCP> <1991Jan21.124531.27867@siesoft.co.uk> <7ejp01gY0c.900@amdahl.uts.a <1069@mwtech.UUCP> Sender: news@cs.vu.nl Reply-To: maart@cs.vu.nl (Maarten Litmaath) Distribution: comp Organization: VU Dept. of Computer Science, Amsterdam, The Netherlands Lines: 38 In article <1069@mwtech.UUCP>, martin@mwtech.UUCP (Martin Weitzel) writes: )[...] )One loophole still remains even in Geoff's solution: embedded newlines in )$string. [...] Here's an example that deals with everything, as far as I know. Enjoy. --------------------cut here-------------------- #!/bin/sh # sed(1) safe substitute example newline=' ' string='this string contains an embedded newline, a backslash, a slash, an ampersand and 2 terminating newlines: \ / & ' nl= case $string in *$newline) nl=$newline esac Xstring=`sed -e 's-[/&\\\\]-\\\\&-g' -e '$!s/$/\\\\/' <