6

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

1

1 Answer 1

5

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.

2
  • 3
    It's not necessary to escape the slashes if you use a different delimiter: sed -n '\|5/Nov/2010|,\|5/Dec/2010|p' Dec 11, 2010 at 0:32
  • Huh, I new you could do that for the s// command, but I didn't realize it worked for searches, too. Cool.
    – larsks
    Dec 11, 2010 at 5:04

You must log in to answer this question.

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