Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!batcomputer!rpi!rpi.edu!tale From: tale@pawl.rpi.edu (David C Lawrence) Newsgroups: comp.unix.questions Subject: Re: Regular Expression delimiters Message-ID: Date: 11 Apr 89 23:29:00 GMT References: <993@n8emr.UUCP> <6710@bsu-cs.bsu.edu> Sender: usenet@rpi.edu Reply-To: tale@pawl.rpi.edu Distribution: comp Lines: 25 In-reply-to: dhesi@bsu-cs.bsu.edu's message of 11 Apr 89 17:30:38 GMT In article <993@n8emr.UUCP> lwv@n8emr.UUCP (Larry W. Virden) writes: Larry> Using sed as a simple example of a complex situation, how would Larry> one code a shell program to invoke sed so that the arguments Larry> passed would always be valid?... Larry> sed -e /$ans/ filename In article <6710@bsu-cs.bsu.edu> dhesi@bsu-cs.bsu.edu (Rahul Dhesi) writes: Rahul> First quote those slashes: Rahul> # works from /bin/sh Rahul> pattern="`echo "$ans" | sed -e 's/\//\\\\\\//g'`" Rahul> sed -e "/$pattern/d" filename Rahul> Count those backslashes carefully. pattern="`echo $ans | sed 'sX/X\\\/Xg'`" a) quoting $ans is only necessary if "-n" could lead it. If you want to allow for that you need to quote it as "`echo \"$ans\" ...`" b) it is generally better to not muddle sed script lines with characters that are appearing inside delimited strings; chosing such a character (as "X", above) means you don't need to add additional quoting levels inside the strings. The above example should make $pattern parseable by sed using / as a delimiter. -- tale@rpitsmts.bitnet, tale%mts@itsgw.rpi.edu, tale@pawl.rpi.edu