Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 25 Sep 2001 09:27:55 -0400
From:      Brian T.Schellenberger <bts@babbleon.org>
To:        Salvo Bartolotta <bartequi@neomedia.it>, Giorgos Keramidas <charon@labs.gr>
Cc:        Mark Rowlands <mark.rowlands@minmail.net>, freebsd-questions@FreeBSD.ORG
Subject:   Re: silly sed question
Message-ID:  <01092509275503.00814@i8k.babbleon.org>
In-Reply-To: <1001421024.3bb078e00bc61@webmail.neomedia.it>
References:  <1001421024.3bb078e00bc61@webmail.neomedia.it>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tuesday 25 September 2001 08:30, Salvo Bartolotta wrote:
> > You are probably being bitten by TCSH's stupid quoting, which I NEVER
> > actually managed to get the grip of.  I tried testing this on sh(1), and
> > here's what I came up with:
> >
> >	$ cat myfile
> >	TARGETS='blob1 blob2 blob3'
> >	$ sed -e "/^TARGETS/ s/\'$/ blob4\'/" myfile
> >	TARGETS='blob1 blob2 blob3 blob4'
>
> Yep, I am afraid you are right: it is tcsh.  And I was somewhat cryptic in
> my previous message.  Sorry.

Here's what I do to work around this.

In  startup scripts, I

setenv S '$'
setenv Q "'"
setenv QQ '"'

Then I do things like this with

sed -e "/^TARGETS/ s/$Q$S/ blob4$Q/" myfile

Good luck.


>
> IIUC, the quoting game should be played as illustrated in the following
> examples:
>
>
> 377 2:10pm ~/trial >====> echo $SHELL
> /bin/tcsh
>
>
> 378 2:11pm ~/trial >====> cat example
> TARGETS='blob1 blob2 blob3'
> 'blob5 blob6 blob7'
>
>
> 379 2:11pm ~/trial >====> sed -e '/^TARGETS=/s/'\''$/ blob4'\''/' example
> TARGETS='blob1 blob2 blob3 blob4'
> 'blob5 blob6 blob7'
>
> # Single quotes: beware of the `'' character.
> # Notice the matching pairs of `''.
>
>
>
> 380 2:11pm ~/trial >====> sed -e "/^TARGETS=/s/'"\$"/ blob4'/" example
> TARGETS='blob1 blob2 blob3 blob4'
> 'blob5 blob6 blob7'
>
> # Double quotes: beware of `$'.
> # Notice the matching pairs of `"'.
> # `'' needs no escaping inside double quotes.
> # In tcsh, this is a simple way to work around the "illegal variable
> error".
>
>
> However, in sh:
>
> $ cat example
> TARGETS='blob1 blob2 blob3'
> 'blob5 blob6 blob7'
>
>
> $ sed -e '/^TARGETS=/s/'\''$/ blob4'\''/' example
> TARGETS='blob1 blob2 blob3 blob4'
> 'blob5 blob6 blob7'
>
> # Single quotes: beware of the `'' character.
> # Notice the matching pairs of `''.
>
>
> $ sed -e "/^TARGETS=/s/'$/ blob4'/" example
> TARGETS='blob1 blob2 blob3 blob4'
> 'blob5 blob6 blob7'
>
> # `'' inside double quotes needs no escaping.
> # A $-keyed variable inside double quotes is normally expanded.
> #
> # Double quotes: in this command, `'' and `$' cause no problem.
> # As to the present example, this is probably the simplest form of the
> command.
>
> -- Salvo (going out and sed'ing someone :-))
>
> To Unsubscribe: send mail to majordomo@FreeBSD.org
> with "unsubscribe freebsd-questions" in the body of the message

-- 
Brian T. Schellenberger . . . . . . .   bts@wnt.sas.com (work)
Brian, the man from Babble-On . . . .   bts@babbleon.org (personal)
                                        http://www.babbleon.org

--------------------> Free Dmitry Sklyarov! <-------------------------

http://www.eff.org                 http://www.programming-freedom.org 

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?01092509275503.00814>