[comp.unix.questions] Need csh alias to match patterns in history

naim@eecs.nwu.edu (Naim Abdullah) (03/24/89)

A friend of mine wants a csh alias that will print all lines in
the history list that match a pattern if one is supplied, otherwise
just print the history.

He asked me and I suggested the following csh alias:

alias h ' \
if ( "X\!*" == "X" ) then \
history \
else \
history | grep \!* \
endif'

For some reason, this does not work. Can some kind soul explain why
and supply a working answer to this problem ?

Thanks.

		      Naim Abdullah
		      Dept. of EECS,
		      Northwestern University

		      Internet: naim@eecs.nwu.edu
		      Uucp: {oddjob, chinet, att}!nucsrl!naim

marcoz@MARCOZ.BOLTZ.CS.CMU.EDU (Marco Zagha) (03/30/89)

In article <3680046@eecs.nwu.edu>, naim@eecs.nwu.edu (Naim Abdullah) writes:
> 
> alias h ' \
> if ( "X\!*" == "X" ) then \
> history \
> else \
> history | grep \!* \
> endif'
> 
> For some reason, this does not work. Can some kind soul explain why
> and supply a working answer to this problem ?

I think this will do the trick:

 alias hh 'history | grep "\!*"'

This works because 'grep ""' is not the same as 'grep'.

Your alias produces strange behavior.  Neither the "if" or the "else"
condition is ever executed.  I don't know why.

== Marco (marcoz@cs.cmu.edu)


-- 

maart@cs.vu.nl (Maarten Litmaath) (03/30/89)

naim@eecs.nwu.edu (Naim Abdullah) writes:
\A friend of mine wants a csh alias that will print all lines in
\the history list that match a pattern if one is supplied, otherwise
\just print the history.

\He asked me and I suggested the following csh alias:

\alias h ' \
\if ( "X\!*" == "X" ) then \
\history \
\else \
\history | grep \!* \
\endif'

\For some reason, this does not work.

Welcome to csh! Generally it doesn't like foreach, while, etc. in aliases.
Solutions:

	alias	h	'set q=(\!*); eval h$#q'
	alias	h0	history
	alias	h1	'(history | sed -n -e \$q -e /"$q"/p)'
or
	alias	h	'if ("\!*" == "") set status=1 &&'\
			'history | sed -n -e \$q -e /"\!*"/p ||'\
			history
or
	alias	h	'if (\!* == "") set status=1 &&'\
			'history | sed -n -e \$q -e /\!*/p ||'\
			history

The final version causes csh to complain if multiple arguments are given.
BTW, it's obvious why I used sed instead of grep.
-- 
 Modeless editors and strong typing:   |Maarten Litmaath @ VU Amsterdam:
   both for people with weak memories. |maart@cs.vu.nl, mcvax!botter!maart