In need of command or script to grep events for last n days
Hello All,
Trying to come up with some command which will grep or egrep (or some other way) the last n days events in a log file. I've already previously grep'd the original file for all the events I'm interested in. Now the final step is just to get the last 7 days events so that I can email the report off. The lines have the following date format: 2013/01/13 15:38:04 So the date will be the variable since obviously this will change every 7 days. Any help would be much appreciated. Chris |
Quote:
One way you could go about doing it is going line by line, extracting the date, and then getting a numerical comparison for the date. e.g. dategrepscript which I just made up (script mixed with pseudo code) Code:
#!/bin/bash Code:
dategrepscript < somelog.log Is this in a single log file or multiple files? If the logs are rotated daily like they should be then it should be as easy as a find command. Code:
find . -type f -name '*.log' -mtime -7 SAM |
Wow - that was quick
sag47,
Thanks for the quick response - I haven't tried anything on my own yet nor your script here but I will give it a shot and let you know. To answer your question: these files are daily files in the form of somefilename.<filedate>.log So what I'm doing is just grep'ing every few days the string I'm interested in and concatenating to a file like so: grep "string" somefilename.* >> dest.file From there i'm sorting and getting rid of dups like so: sort -u dest.file > uniq-dest.file Then from there, I'll try and use your script (thank you) and hope it works. I need to learn how to do script. Chris |
Well if they're in separate files then you could try something like this as well.
Code:
find . -type f -name 'somefilename.*' -mtime -7 -exec grep 'string' {} \; Recursively find in the current directory (.) a file of type file (-type f) which matches a name (-name ...) that is younger than the past seven days (-mtime -7). Then when a said file is found it will then execute the grep command on that particular file. In the case of multiple files, it will execute grep individually on each one as it encounters them searching for 'string' (-exec ...). NOTE: Not all versions of find can handle plus/minus seven with mtime (-mtime -7) so YMMV depending on your Unix/Linux flavor and version. Bash scripting is a good skill to learn and I'd recommend it. I also recommend reading and fully understanding any script (and all of the options of the commands therein) a forum user gives you. While it is not usually intended, hack-a-day scripts can damage a production system if not properly reviewed for errors or malicious code. SAM |
thank you again
sag47 - you are a good Samaritan and thank you again. Do you have any good bash reference tutorial you would recommend online or a book ?
|
Quote:
A quick tip most text books fail at teaching people... One thing you should note to yourself when checking out scripts and remembering how the "if" conditionals work is that [ is a program (namely /usr/bin/[) which is also called test (man test). /usr/bin/[ outputs a zero if the arguments are evaluated true and a non-zero if the evaluated expression is false. Since /usr/bin/[ or just [ is a program this is why it requires spaces for the expression because they're all arguments (which is why [5 -gt 4] doesn't work). e.g. Code:
/usr/bin/[ 5 -gt 4 ] e.g. Code:
if curl --connect-timeout 1 http://derpserver.com;then SAM |
man - you're awfully generous with you time - thanks again for your help.
|
Good bash tutorial http://rute.2038bug.com/index.html.gz
|
All times are GMT -5. The time now is 03:57 PM. |