tags:

views:

52

answers:

4

I want to cat an apache log and then output the result to a file. I want to match the day/month with the Apache log to the current/previous date, however I seem to be doing this incorrectly.

Sample from apache log: 62.173.92.62 - - [02/Mar/2010:15:46:58 +0000] "GET /img.......

Current script:

cat access_log | grep "\[+%d+/%b" > email.log

Which I was hoping would match the [0/2Mar part of the line, however I am getting nothing in email.log (permissions are ok).

Thanks

A: 

grep doesn't work that way; it doesn't care what the current date is, it uses the regular expression you pass it blindly. Use date with an appropriate format to get the value you care about, and use that with grep.

Ignacio Vazquez-Abrams
That is, try `grep $(date +\[%d/%b) access_log`. (Note that you don't need the `cat`; `grep` takes arguments.
Jefromi
A: 

Try something like:

day=`date +%d`
mon=`date +%b`
grep "\[$day/$mon/" access_log
Paul
You can specify multiple placeholder with date, eg. date +%d/%b
ar
+3  A: 

Try this instead, you need to call date and pass its output into the pattern. Also the [ needs to be escaped.

grep "\[$(date +%d/%b/%Y)"
ar
I would vote this up, but don't have any rep. Thank you- worked perfectly!
aligibbs
@aligibbs: You only need to be logged in to vote up answers - it is voting up comments and voting down that needs rep.
Charles Stewart
A: 

for current

grep "\[$(date +%d/%b/%Y)" file > email.log

for previous

grep "\[$(date +%d/%b/%Y -d 'yesterday')" file > email.log

No need cat.

ghostdog74