Search within a log file within a time Range
Hi,
I want to search in my apache log, for events which have occurred say between 11:00 AM to 2:00 PM. I have got few scripts/commands but they are not conclusive, some of then are trying to do an exact match(awk) and for some i am just getting the pattern wrong (eGrep) I am running RHEL. Apache log eg: ================================================================ 10.92.120.129 - - [24/Jun/2011:01:29:07 -0500] "GET /favicon.ico HTTP/1.1" 302 ================================================================ now if i want to find out the logs from 1 PM to 2 PM some example i had was: cat <apache.log> | grep -E '07\/Apr\/2011:(0[1-2]:[0-5][0-9]|02:00)' Can someone help on this, for a small script in which i can pass the start time and end time range, and it should give an output of the rows which fall under that time range. Regards |
Code:
#!/bin/bash |
still not working..
1 Attachment(s)
Thankyou colucix,
I tried the script but i am not able to get any result, attaching the script and the apache log which i am using to test the script. ============================================================= #!/bin/bash d1=$(date -d "20110611 04:15:00" +%s) d2=$(date -d "20110611 04:20:00" +%s) while read line do date=$(echo $line | awk 'BEGIN{ FS = "[][]" }{ gsub(/\//," ",$2); sub(/:/," ",$2); sub(/-.*/,"",$2); print $2 }') date=$(date -d "$date" +%s) [[ $date -ge $d1 && $date -le $d2 ]] && echo $line done < /home_dir/a038010/apache.log ============================================================= Can you please have a look, if like i am doing something wrong. Regards Saurav |
Dear colucix,
I have finally made it work, it seems i was doing something wrong. It works perfectly, thanks a ton !!! here is the working script for me, broke the original script and rebuilt it to understand the working... not much of a change though, added few comments Quote:
|
Edit: (forget what I posted here at the beginning before; just discovered my error. :doh:)
I did a bit of rewriting, replacing the awk command with a few parameter expansions, so now it depends on no external commands other than date. I also altered it to accept the parameters as script arguments. Code:
#!/bin/bash By the way, please use [code][/code] tags around your code, to preserve formatting and to improve readability. Don't use [quote][/quote] tags for code blocks, as they don't preserve formatting. And for goodness sake don't use unbroken lines of "====" or any other character, as they can cause the page width to expand and force side-scrolling. Thanks! |
Apologies for a delayed reply, was busy with too many stuff.
Thank~you very much David for the updated Code, all your points taken, this script is very helpful. I can customize it further if required to search other logs with different date formats. Thanks Again |
All times are GMT -5. The time now is 11:59 PM. |