Linux – grep the specific date patterns

awkgreplinux

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

Best Answer

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
Related Question