[Tutor] How to compare dates which has (,)
Fosiul Alam
fosiul at gmail.com
Sun Mar 6 13:39:54 EST 2016
Hi ,
my bellow codes work ok to compare date and time for perticular format
example
Feb 7 07:33:19 ,
but I am not understanding how do i capture and compare dates in bellow
formate (,) :
<Mar 4, 2016 5:38:58 AM BRT>
also, i having trouble to implement this in python : grep -E -A5 -B3
So basically what i am want is :
SCAN all log files in last 30 minutes for key word like ERROR and give me
out put like grep -E -A5 -B3
<Mar 4, 2016 5:38:58 AM BRT> <Warning> <J2EE> <BEA-160140> <aaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.>
<Mar 4, 2016 5:39:00 AM BRT> <Warning> bbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbb.>
<Mar 4, 2016 5:39:00 AM BRT> <Warning> bbbbbbbbbbbbbbbbbbbbbbbbbbccc
ccccccccccccccccccccccccc..>
My curent code which works for bellow format :
#Feb 7 07:33:19 xxxx yum[17961]: Installed: vsftpd-2.2.2-14.el6.x86_64
get_date = re.compile('([A-Z-a-z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+)(.*)')
current_time = datetime.datetime.now()
lastHourTime = datetime.datetime.now() - datetime.timedelta(hours = 1)
def _read_log():
with open (LOG_FILE,'r')as f:
content=f.readlines()
return content
if __name__ == '__main__':
log_file=_read_log()
last_execution_time=_get_last_log_time()
for line in log_file:
#GEt the Date only from the log file Feb 7 07:33:19
match=re.search(get_date,line)
if match:
#Capture only the date field so taht we can compare iet
with (current_time and lastHourTime.
#log_date= match.group(1).rstrip()
log_date =
datetime.datetime.strptime(match.group(1).rstrip(), "%b %d
%H:%M:%S").replace(year=datetime.date.today().year)
#log_date='Feb 7 07:33:19'
#print ('Log Date %s' %log_date)
#Check if log_date is greater then lastHourTime and less
then current_time
if log_date < current_time and log_date > lastHourTime :
new_log.append(line)
print new_log
any help will be really appreciate
Thanks
More information about the Tutor
mailing list