[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Sheflug] command as argument for grep



> 
> I have the following line
> 
> cat index.html |grep 'date %d-%b-%Y
> 
> which I thought would return all the lines in index.html that contained
> today's date.  But it doesn't.  Can someone please remind me how to pass the
> output of a command as an argument to a previous command.
> 

You need to use backticks ... in my keyboard, its the key located to the 
left of the '1' key, above tab. Also, I think you're missing a '+' from
in front of the date format (certainly, without '+' it doesn't work on
by box).

Btw, you don't need cat in this case (this is a UUoC[1]), as you can 
rewrite as:

	grep `date +%d-%b-%Y` index.html

In some shells (ksh (possibly bash?)), you can also use $(...), e.g.:

	grep $(date +%d-%b-%Y) index.html

This latter form is doubly useful as it can be nested -- backticks OTOH
cannot be nested.

If the command you're executing returns something with spaces, remember
to quote accordingly -- this uses both backticks and double quotes:

	grep "`date +%d %b %Y`" index.html

Chris...

[1] aka, Useless Use of Cat - originally from the newsgroups
	comp.unix.shell and comp.unix.admin

-- 
\ Chris Johnson           \
 \ cej [at] nightwolf.org.uk    ~-----,   
  \ http://cej.nightwolf.org.uk/  ~-----------------------------------, 
   \ Redclaw chat - http://redclaw.org.uk - telnet redclaw.org.uk 2000 \____
___________________________________________________________________

Sheffield Linux User's Group -
http://www.sheflug.co.uk/mailfaq.html

  GNU the choice of a complete generation.