[comp.unix.questions] quoting $ in the Csh

schmidt@siam.ics.uci.edu (Doug Schmidt) (12/11/88)

Hi,

   I'm attempting to create an csh alias that will filter out all
blank lines from a given file ( i.e., replace a sequence of blank
lines with *no* blank lines ).  The following grep argument does the
trick:

grep -v '^[ ^I]*$'  # the ^I stands for tab

However, when I attempt to make this a csh alias, e.g.:

alias sqz "grep -v '^[ ^I]*$'"

The cshell complains about ``variable syntax,'' since it appears to be
attempting to expand $'.  I've tried escaping the $, and that doesn't
help.  I've tried putting an extra space between the $ and ', but that
screws up the regular expression.  

Does anyone know of a good method for getting this to work?!

thanks,

Doug Schmidt
--
schmidt@ics.uci.edu (ARPA) |   Per me si va nella citta' dolente.
                           |   Per me si va nell'eterno dolore.
                           |   Per me si va tra la perduta gente.
                           |   Lasciate ogni speranza o voi ch'entrate.

maart@cs.vu.nl (Maarten Litmaath) (12/13/88)

schmidt@siam.ics.uci.edu (Doug Schmidt) writes:
\alias sqz "grep -v '^[ ^I]*$'"

\The cshell complains about ``variable syntax,'' since it appears to be
\attempting to expand $'.

Csh's syntax + parser are SICK! Csh is an example of `loving your enemy' :-)

The following trick will do it:

	alias	sqz	"grep -v '^[ ^I]*'"$

A `lonely' $ is left alone.
-- 
fcntl(fd, F_SETFL, FNDELAY):          |Maarten Litmaath @ VU Amsterdam:
      let's go weepin' in the corner! |maart@cs.vu.nl, mcvax!botter!maart

mirk@warwick.UUCP (Mike Taylor) (12/13/88)

In article <1799@solo9.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes:
>Csh's syntax + parser are SICK! Csh is an example of `loving your enemy' :-)

How right you are - csh is beautiful for interactive use, but a real pain
when it comes to working out tricky quotations.  Maybe best of all is when
you get yourself a nice little one-liner awk-script, and try to make that
into an alias.  Cos of course, the trouble is, you have to protect your
true meaning in such a way that not only do you quote all your meta-
characters, but that you also quote the characters that quote them in such
a way that they will be able to continue to do so after the initial layer
of quoting has been stripped off during the "alias" command itself!  (Phew!)

This is generally OK if your expression has no single-quotes in it, in which
case you can just wrap it all up in single-quotes.  If it _does_ have any,
then you hit big trouble, because as our on-line csh(1) manual accurately
points out in its BUGS section:

	Quoting conventions and contradictory and confusing.

A simple example?  OK.
Suppose you want to know how many news articles you have:  Simple.
Generate a list of the first 999 newsgroups (should be enough! :-) together
with a count of the articles in each, using "rn -c -s999".  Then feed the
result to awk '{x+=$5}; END {print x}', which prints the sum of the 5th
field of each line.

Now try to build and alias for it?

	alias cart 'rn -c -s999 | awk '{x+=$5}; END {print x}''

Thjis won't work because csh(1) gets confused about which single quotes
belong to the "alias" statement and which belong to "awk".  (What an
awful explaination.  Never mind.  You know what I mean).

So maybe we try:

	alias cart 'rn -c -s999 | awk \'{x+=$5}; END {print x}\''

No deal.  I can't say exactly what is going wrong here, but for some reason,
(Any of you Toreks/Spencers out there like to explain) csh(1) decides that
quoting the inner 's with \ isn't good enough, and spits back a "Unmatched
single quote" error at you.

So try using double quotes to surround the alias - better, but not good enough,
because remember there is no way to prevent variable substitution taking place
within double quotes.  To cut a long story short(er), here is what I ended
up with, ensuring that the $ is not enclosed in double quotes, but in
single ones:

	alias cart "rn -c -s999 | awk '{x+="'$5'"}; END {print x}'"

Cruddy, huh?  There must be a better way to do these things.
Ah well, until that day ...
______________________________________________________________________________
Mike Taylor - {Christ,M{athemat,us}ic}ian ...  Email to: mirk@uk.ac.warwick.cs
*** Unkle Mirk sez: "Gm F#7 Bb Cm->Gm->Eb->Gm F; Gm F#7 Bb C Eb C Bb Dm D" ***
------------------------------------------------------------------------------

pjh@otter.hpl.hp.com (Patrick Hyland) (12/14/88)

The following does what you want (but will win few prizes for subtlety):

   alias sqz grep\ -v\ \'^\[\ \^I\]\*\$\'

							Patrick Hyland.

schmidt@siam.ics.uci.edu (Doug Schmidt) (12/14/88)

I'd like to thank everyone who responded to my question about quoting
'$' in the csh.  The following is a brief summary of all the
different, valid responses from various folks ( the original problem
was to create an alias that would replace all blank lines from a file
with *no* blank lines ).

----------------------------------------
alias sqz "grep -v '^[ ^I]*"\$\'
----------------------------------------
alias sqz grep -v \'^\[\ \^I\]\*\$\'
----------------------------------------
alias sqz "grep -v '^[ ^I]*"\$"'"
----------------------------------------
alias sqz "grep -v '"'^[ ^I]*$'"'"
----------------------------------------
alias sqz 'grep -v \'^[ ^I]*$\''
----------------------------------------
alias sqz "grep -v '^[ ^I]*"'$'"'"
----------------------------------------
alias sqz 'grep -v '\''^[ ^I]*$'\'
----------------------------------------
set d='$'
alias sqz "grep -v '^[ ^I]*$d' \!*"
----------------------------------------

Rather diverse approaches, eh? ;-)

thanks again.

Doug Schmidt
--
schmidt@ics.uci.edu (ARPA) |   Per me si va nella citta' dolente.
                           |   Per me si va nell'eterno dolore.
                           |   Per me si va tra la perduta gente.
                           |   Lasciate ogni speranza o voi ch'entrate.