开发者

Bash script not quite matching

开发者 https://www.devze.com 2022-12-21 21:40 出处:网络
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 incorre

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号