This is the code:
#!/usr/bin/env python
#Import the datetime
from datetime import datetime
import re
#Create two datetime object for limit 1 and limit 2 as dt1 and dt2 respectively
dt1 = datetime.strptime("01:00:00","%H:%M:%S").time()
dt2 = datetime.strptime("04:59:59","%H:%M:%S").time()
#Create a compiler for regular expression
init_re = re.compile(r'(INIT)')
time_re = re.compile(r'(\d+:\d+:\d+)')
# read line from test.log file
for line in open("test.log", "r"):
match = time_re.search(line) #Search time format for each line
if match:
matchtime = match.group(1)
dt_match = datetime.strptime( matchtime, '%H:%M:%S').time()
#Time formmat match
if dt_match >= dt1 and dt_match <= dt2:
match1 = init_re.search(line) #search INIT format
if match1:
matchinit = match1.group(0)
print match.string.strip()
Below is a partial part of the log file:
2015-12-15 00:51:01,904 INFO restser.py 113 [INIT] [netkv_restser: peek] [req_id: f0aa7ab5-6192-4231-93cd-82a53936a072] [request: {u'key_space': u'martech_user_index', u'table_name': u'nettopic', u'key': 9569}]
I want output like this:
[Date: ] [Time: ] [INIT] [netkv_restser: ] [req_id: ]
Example:
[Date:2015-12-15 ] [Time:00:51:01,904 ] [INIT] [netkv_restser:peek ] [req_id:f0aa7ab5-6192-4231-93cd-82a53936a072 ]
Note: If you are nice enough to edit the code, be kind enough to provide solutions. I don't wanna sound rude but it irks me.
NOTE: I am using Python 2.7.6.