[SOLVED] Search within a log file within a time Range
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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.
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:
#!/bin/bash
#Please enter the time range in the given format, d1 is start time, d2 is end time
d1=$(date -d "20110606 04:17:00" +%s)
d2=$(date -d "20110606 04:18: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)
if [[ $date -ge $d1 && $date -le $d2 ]]
then
# The below line displays the line which falls in between the time/date range given
# use a grep to filter any particular match in the final result
echo $line
fi
Edit: (forget what I posted here at the beginning before; just discovered my error. )
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
# Usage: scriptname [date1] [date2] logname
# dates should be in the same format as the apache log
# although other formats supported by the date command may work
# (but probably won't since the function strips a single colon from it)
cleandate() {
local n
# convert the month to numeric form
case "$1" in
*Jan* ) n="${1/Jan/01}" ;;
*Feb* ) n="${1/Feb/02}" ;;
*Mar* ) n="${1/Mar/03}" ;;
*Apr* ) n="${1/Apr/04}" ;;
*May* ) n="${1/May/05}" ;;
*Jun* ) n="${1/Jun/06}" ;;
*Jul* ) n="${1/Jul/07}" ;;
*Aug* ) n="${1/Aug/08}" ;;
*Sep* ) n="${1/Sep/09}" ;;
*Oct* ) n="${1/Oct/10}" ;;
*Nov* ) n="${1/Nov/11}" ;;
*Dec* ) n="${1/Dec/12}" ;;
esac
# remove first colon from string
n="${n/:/ }"
#use date to convert to epoch and print
date -d "$n" +%s
}
# Take the input dates and process them to epoch with the cleandate function
d1=$( cleandate "$1" )
d2=$( cleandate "$2" )
while read line; do
# Extract the date for each line.
# First strip off everything up to the first "[".
# Then remove everything after the first "]".
# Finally, straighten up the format with the cleandate function
date="${line#*[}"
date="${date%%]*}"
date=$( cleandate "$date" )
# If the date falls between d1 and d2, print it
if [[ $date -ge $d1 && $date -le $d2 ]]; then
echo "$line"
fi
done < "$3"
exit 0
It could probably be made more robust, such as making it able to handle multiple date formats.
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!
Last edited by David the H.; 06-28-2011 at 09:32 AM.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.