grep range of dates - apache log

I'm wondering how could I do the following greps:

  • grep Apache log only for a range of dates, assume from 5/Nov/2010 to 5/Dec/2010

and

  • grep Apache log starting from `15/Nov/2010 until the last log entry.

Thanks


Solution 1:

I'm going to answer these in reverse order:

grep Apache log starting from `15/Nov/2010 until the last log entry.

This doesn't require any sort of date parsing and can be accomplished easily with sed:

# sed -n '/15\/Nov\/2010/,$ p' /path/to/access_log

This tells said to show lines (p) starting with 15/Nov/2010 through the end of the file ($).

grep Apache log only for a range of dates, assume from 5/Nov/2010 to 5/Dec/2010

This is almost the same, but instead of showing lines through the end of the file, you want to stop printing lines when you reach the second date. You could try something like this:

# sed -n '/5\/Nov\/2010/,/5\/Dec\/2010/ p'

Note that in both of these examples I'm escaping the / characters because sed uses these to delimit regular expressions.