Regular Expression delimiters

David C Lawrence tale at pawl.rpi.edu
Wed Apr 12 09:29:00 AEST 1989


In article <993 at n8emr.UUCP> lwv at 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 at bsu-cs.bsu.edu> dhesi at 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 at rpitsmts.bitnet, tale%mts at itsgw.rpi.edu, tale at pawl.rpi.edu



More information about the Comp.unix.questions mailing list