grep the specific date patterns

0

2

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

maneeshshetty

Posted 2011-04-05T17:58:41.737

Reputation: 572

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 – 2011-04-05T19:22:17.987

1useless use of cat: egrep "pattern..." /var/log/messages – glenn jackman – 2011-04-05T19:35:32.913

@GlennJackman has proved me shortsighted. Definitely and improvement in awk! – shellter – 2011-04-05T20:30:16.867

Answers

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

glenn jackman

Posted 2011-04-05T17:58:41.737

Reputation: 18 546

Yes, this is what I was looking for. Thanks Glenn! – maneeshshetty – 2011-04-05T20:42:30.497