Operating System - HP-UX
1833407 Members
3071 Online
110052 Solutions
New Discussion

Re: Problem with passing date argument to grep

 
SOLVED
Go to solution
Rootberry
Advisor

Problem with passing date argument to grep

I am trying to create an alias command and am having issues. I want a one line command so that I can do a longlisting and grep for todays date and date. So if I do a ll | grep `date "+%b %d"` I get can't open 26 which is todays date. But if I take off %d it pulles up everything in the directory in the month of Sep. I've tried using argument as seen below and get nothing, no errors. Any advice would be appreciated.

MyDate=$1
MyDate=`date '+%b %d'`
dir | grep `echo '$MyDate'`
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: Problem with passing date argument to grep

Rethink the entire process. Grep will match anywhere in the line so for example if "Sep" is part of the name of the file, it will match. You could play around will awk to split the fields out but I would really do this in Perl because ls's output varies depending upon the age of the file. You could also use find . -mtime 1, for example.
If it ain't broke, I can fix that.
spex
Honored Contributor
Solution

Re: Problem with passing date argument to grep

Hi,

# ls -l | grep " $(date '+%b %d') "

PCS
James R. Ferguson
Acclaimed Contributor

Re: Problem with passing date argument to grep

Hi:

# alias doit='X=`date "+%b %d"` ll|grep "${X}"'

...of course, you can think of better alias names than 'doit'...

Regards!

...JRF...
Rootberry
Advisor

Re: Problem with passing date argument to grep

Thank you both ended up working great. I went ahead and used this one combined with my dir command. Made the command name dirg, short for dir grep :D


root[/usr/bin] >pg dir
ls -ail $* | more

root[/usr/bin] >pg dirg
TodaysDate=`date "+%b %d"`
dir | grep "${TodaysDate}"
Rootberry
Advisor

Re: Problem with passing date argument to grep

See above