2

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

0

4 Answers 4

3

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)"
Sign up to request clarification or add additional context in comments.

2 Comments

I would vote this up, but don't have any rep. Thank you- worked perfectly!
@aligibbs: You only need to be logged in to vote up answers - it is voting up comments and voting down that needs rep.
0

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.

1 Comment

That is, try grep $(date +\[%d/%b) access_log. (Note that you don't need the cat; grep takes arguments.
0

Try something like:

day=`date +%d`
mon=`date +%b`
grep "\[$day/$mon/" access_log

1 Comment

You can specify multiple placeholder with date, eg. date +%d/%b
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.