Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
1  
have a look at the answers to this question serverfault.com/questions/101744/… –  Iain Dec 10 '10 at 18:36
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
That was EXACTLY what I was looking for, thanks! –  Charles Dec 10 '10 at 18:54
2  
It's not necessary to escape the slashes if you use a different delimiter: sed -n '\|5/Nov/2010|,\|5/Dec/2010|p' –  Dennis Williamson Dec 11 '10 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 '10 at 5:04
add comment

Your Answer

 
discard

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.