Register
It is currently Sat Nov 01, 2014 1:37 am

Timestamp Search


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Tue Aug 05, 2008 12:51 pm   

Joined: Tue Aug 05, 2008 12:46 pm
Posts: 2
Hi,

I am trying to write a script which would go search and get the info from the logs based on START and END timestamps. The log file format is as follows

255.45.250.227 - Unauth [05/Aug/2008:20:45:05 -0400] "GET HTTPS://relia.stage.tata.com/include...Javascript.jsp HTTP/2.2" 200 46850
255.45.250.227 - Unauth [05/Aug/2008:20:48:05 -0400] "GET HTTPS://relia.stage.tata.com/un/js/browsercheck.js HTTP/2.2" 504 0
244.258.27.225 - airtel1@tata.net [05/Aug/2008:20:50:05 -0400] "GET HTTPS://test-Sky.stage.tata.com/stat...m_writeHTML.js HTTP/2.2" 200 2702
255.45.250.227 - Unauth [05/Aug/2008:20:52:05 -0400] "GET HTTPS://relia.stage.tata.com/un/js/general.js HTTP/2.2" 504 0
255.45.250.227 - Unauth [05/Aug/2008:20:55:04 -0400] "GET HTTPS://relia.stage.tata.com/un/js/lsgeneralScripts.js HTTP/2.2" 504 0


echo -n "3a. Timestamp start = "
read time_st
echo -n "3b. Timestamp end = "
read time_en

----
3a. Timestamp start = 05/Aug/2008:20:45:05
3b. Timestamp end = 05/Aug/2008:20:55:04
----

The timestamp format in the logs is as follows 05/Aug/2008:20:55:04.


Thank you


Top
 Profile  
 PostPosted: Tue Aug 05, 2008 3:48 pm   

Joined: Tue Aug 05, 2008 12:46 pm
Posts: 2
I used

sed -n '/start/,/end/ p' file but couldnt get the desired output.


Top
 Profile  
 PostPosted: Tue Aug 05, 2008 5:57 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
if you had the line in a variable say... bob... you could use the code below:
Code:
newbob=`echo "$bob" | cut -d[ -f2 | cut -d] -f1 | cut -d" " -f1`
echo "$newbob"


a little odd... but i love using cut :)


Top
 Profile  
 PostPosted: Wed Aug 27, 2008 9:01 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Might be a little late, but have you tried awk?

Code:
# awk '/START/,/END/' file


I tried a test with the dates formatted the same as yours, and this is what worked for me:

Code:
awk '/27\/Aug\/2008:11:46:58/,/27\/Aug\/2008:11:48:01/' tmp.log


You have to escape the slashes, but that should be easy to code around. There's probably a more elegant solution, but this sed line should do the job...

Code:
# sed s/\\//\\\\\\//g


So here's a sample script that I wrote just to make sure I wasn't crazy ;) Note that the "-e" option for echo isn't available (or isn't needed? not sure...) on all flavors of Linux, but i really only used it to get the extra "\n" in there...

Code:
#!/bin/sh

START="$(echo $1|sed s/\\//\\\\\\//g)"
echo -e "Start time variable is: \"$START\""

END="$(echo $2|sed s/\\//\\\\\\//g)"
echo -e "End time variable is: \"$END\"\n"

awk "/$START/,/$END/" tmp.log


And here's the output! Is this what you were looking for?

Code:
$ sh ./date-range.sh "27/Aug/2008:11:47:00" "27/Aug/2008:11:47:14"

Start time variable is: "27\/Aug\/2008:11:47:00"
End time variable is: "27\/Aug\/2008:11:47:14"

27/Aug/2008:11:47:00 [rowlf] The quick brown fox jumped over the lazy dog
27/Aug/2008:11:47:01 [rowlf] The quick brown fox jumped over the lazy dog
27/Aug/2008:11:47:01 [rowlf] The quick brown fox jumped over the lazy dog
27/Aug/2008:11:47:13 [rowlf] The quick brown fox jumped over the lazy dog
27/Aug/2008:11:47:14 [rowlf] The quick brown fox jumped over the lazy dog


I hope this helps!
-Jeo


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Yahoo [Bot] and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP