Grep syntax questions
Hey all,
Just a general question about the use of the grep command. I can never seem to get it to work quite right, and the man pages for it don't really explain it very well. What I'm trying to do is have a daily job set up to pull the previous days worth of entries from various log files (httpd, system logs, etc). I try to test out various syntax combinations, but I can't ever seem to get grep to show just 1 days worth when sorting by date. So, for example, the Apache logs store the date like this: Code:
[10/Apr/2005:04:07:53 -0500] Code:
grep "date --date=yesterday '+%d/%b/%G/%T'" /var/log/httpd/access_log Also, if I can get my results to come back properly, I'd like to write them out to a text file with the date as the filename. I believe grep can pipe it to a file, but again, I'm not sure on the syntax. Can I just echo it to a file, or is there a better way? If it helps any, this job will be running on a Fedora 3 system. Any help on this will really be appreciated. |
As a starting point change your date syntax, this is your current one:-
Code:
date --date=yesterday '+%d/%b/%G/%T' Code:
date --date=yesterday '+%d/%b/%G:%T' |
Hi!
I think that it could work if you tried to put $( ) around the thing that should return a string. like this: grep "$(date --date=yesterday '+%d/%b/%G/%T')" /var/log/httpd/access_log does that work? |
Ok, it appears that I did in fact need the $( ) to return the values properly. I also took out the %T search variable to get it to work, thinking that it was only searching for the exact time, down to the second, as it is when the job runs. So, now my search string looks like this:
Code:
grep "$(date --date=yesterday '+%d/%b/%G')" /var/log/httpd/access_log Now, how difficult will it be to write this to a text file at the same time? Thanks! |
if you want it written to a file as well as the terminal, use the "tee" command. See "man tee" for the options available:-
Code:
grep "$(date --date=yesterday '+%d/%b/%G')" /var/log/httpd/access_log | tee -ai /path/file Code:
grep "$(date --date=yesterday '+%d/%b/%G')" /var/log/httpd/access_log > /path/file Code:
grep "$(date --date=yesterday '+%d/%b/%G')" /var/log/httpd/access_log >> /path/file |
All times are GMT -5. The time now is 11:25 AM. |