[Tutor] How to compare dates which has (,)

Fosiul Alam fosiul at gmail.com
Thu Mar 10 06:28:52 EST 2016


Hi,
I am using
Python 2.6.6 (r266:84292, Sep 12 2011, 14:03:14)
[GCC 4.4.5 20110214 (Red Hat 4.4.5-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>



On Tue, Mar 8, 2016 at 1:38 PM, Whom Isac <wombingsac at gmail.com> wrote:

> And it would be helpful if you could provide us about which version of
> python you are using, does your code runs an error, format/structure of
> your log file and what does your current code returns when you run it. By
> the way, if you had imported datetime as :=> import datetime then you don't
> have to repeat datetime.datetime.now().
>
> On Tue, Mar 8, 2016 at 11:32 PM, Whom Isac <wombingsac at gmail.com> wrote:
>
>> Hi, I am not too good with data handling process but my advice to you to
>> check any pickle/cpickle module which may have any function to scan, read
>> and write your log files. If not then try the old good Json. These modules
>> are good data extraction and data writing.
>>
>> On Mon, Mar 7, 2016 at 4:39 AM, Fosiul Alam <fosiul at gmail.com> wrote:
>>
>>> 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
>>> _______________________________________________
>>> Tutor maillist  -  Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> https://mail.python.org/mailman/listinfo/tutor
>>>
>>
>>
>


-- 
Regards
Fosiul Alam


More information about the Tutor mailing list