0

I have a requirement, where i need to grep from the logs which covers from date range 2011/03/25 to 2011/04/04 (YYMMDD).

I have done this with egrep command, seems to be working fine for me.

cat /var/log/messages | egrep "2011/03/2[6-9]|2011/03/3[0-1]|2011/04/0[1-4]"

I was looking for awk command which would make my life simple.

Below is the snippet of the log file ...

 2011/04/01-12:15:00
 2011/04/01-12:15:00
 2011/04/01-12:15:00
 2011/04/01-12:15:01
 2011/04/01-12:15:01
 2011/04/01-12:15:01
 2011/04/01-12:15:01
 2011/04/01-12:15:01
3
  • Why do you think it will be significantly different in awk? You will defintely need a reg-expr where the '/' chars are escaped as you have them, 2011\/04.... But I don't think you need to escape '/' in an egrep (at least on the version I use). Good luck!
    – shellter
    Apr 5, 2011 at 19:22
  • 1
    useless use of cat: egrep "pattern..." /var/log/messages Apr 5, 2011 at 19:35
  • @GlennJackman has proved me shortsighted. Definitely and improvement in awk!
    – shellter
    Apr 5, 2011 at 20:30

1 Answer 1

1
awk -F - '"2011/03/25" < $1 && $1 <= "2011/04/04"' filename

or, if the dates are more dynamic

d1="2011/03/25" # or whatever commands to set this date
d2="2011/04/04" # or whatever commands to set this date
awk -v start=$d1 -v end=$d2 -F - 'start < $1 && $1 <= end' filename
1
  • Yes, this is what I was looking for. Thanks Glenn! Apr 5, 2011 at 20:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .