Bash script not quite matching
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
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)"
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
.
Try something like:
day=`date +%d`
mon=`date +%b`
grep "\[$day/$mon/" access_log
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.
精彩评论