Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ...

share|improve this question
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 '11 at 19:22
useless use of cat: egrep "pattern..." /var/log/messages – glenn jackman Apr 5 '11 at 19:35
@GlennJackman has proved me shortsighted. Definitely and improvement in awk! – shellter Apr 5 '11 at 20:30
up vote 1 down vote accepted
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
share|improve this answer
Yes, this is what I was looking for. Thanks Glenn! – maneeshshetty Apr 5 '11 at 20:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.