Python Print lines for the closest timestamp of the event

0 votes

I need the following output :


File caused while executing ?/rdbms/admin/prvtlock.plb
Corresponding Error found in apply log at time 2018-10-17 12:28:12
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 
create or replace package body dbms_lock wrapped
*
ERROR at line 1:
ORA-04021: timeout occurred while waiting to lock object SYS.DBMS_LOCK
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 

Here's what I tried but it didn't work:

import re
import os
import datetime
from datetime import timedelta

f3 = open (r"file1", 'r' )
st=f3.read()

if re.search('ORA-04021(.*)lock(.*)',st):
    #y = re.findall ( "(\w{3})\s+([0-9]{2})\s+(\d+):(\d+):(\d+)\s+(\d+)", st )

    y=re.findall("[A-Z][a-z][a-z] [ 123][0-9] [012][0-9]:[0-5][0-9]:[0-5][0-9] [0-9][0-9][0-9][0-9]",st)
    y1 = datetime.datetime.strptime(y[0], '%b %d %H:%M:%S %Y')
    y2 = datetime.datetime.strptime(y[1], '%b %d %H:%M:%S %Y')
    print "Start time: ",y1
    print "End time: ",y2

f4 = open (r"file2", 'r')

string1=f4.readlines()
#print(string1)
for i in range(len(string1)):
  if re.search ( r'\d\d\.\d\d\.\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]', string1[i] ):
    a = re.findall (r'\d\d\.\d\d\.\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]', string1[i] )
    #print a,i,type(a)
    newtime = datetime.datetime.strptime ( a[0], '%d.%m.%y %H:%M:%S' )
    print newtime
    if newtime > y1 and newtime < y2:
       if re.search ( r'ORA-04021', string1[i] ):
          position=i
          lastposition =position - 3
          b = position
          while True:
               if re.search('INSTALL_FILE',string1[b]):
                  print "\n Error caused while executing ",string1[b + 2]
                  break
              #elif lastposition==len(string1)-1:
              #    break
               else:
Jan 2, 2019 in Python by slayer
• 29,370 points
1,175 views

1 answer to this question.

0 votes

Try this:

import re
import os
import datetime
from datetime import timedelta

f3 = open (r"D:\QI\logfile_for_perl_beginners\file1.log", 'r' )
st=f3.read()

if re.search('ORA-04021(.*)lock(.*)',st):
    #y = re.findall ( "(\w{3})\s+([0-9]{2})\s+(\d+):(\d+):(\d+)\s+(\d+)", st )

    y=re.findall("[A-Z][a-z][a-z] [ 123][0-9] [012][0-9]:[0-5][0-9]:[0-5][0-9] [0-9][0-9][0-9][0-9]",st)
    y1 = datetime.datetime.strptime(y[0], '%b %d %H:%M:%S %Y')
    y2 = datetime.datetime.strptime(y[1], '%b %d %H:%M:%S %Y')
    print "Start time: ",y1
    print "End time: ",y2

f4 = open (r"D:\QI\logfile_for_perl_beginners\file2.log", 'r')

string1=f4.readlines()
#print(string1)
for i in range(len(string1)):
  if re.search ( r'\d\d\.\d\d\.\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]', string1[i] ):
    a = re.findall (r'\d\d\.\d\d\.\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]', string1[i] )
    #print a,i,type(a)
    newtime = datetime.datetime.strptime ( a[0], '%d.%m.%y %H:%M:%S' )
    #print newtime
    if newtime > y1 and newtime < y2:
          position=i
          lastposition =position+1
          while True:
               if re.search('Calling rdbms/admin',string1[lastposition]):
                  break
               elif lastposition==len(string1)-1:
                  break
               else:
                  lastposition += 1

          errorcheck=string1[position:lastposition]
          #print "Corresponding Error found in apply log"
          for i in range ( len ( errorcheck ) ):
              if re.search ( r'ORA-04021', errorcheck[i] ):
                  print "Reason of error \n", errorcheck[i]
                  print "script \n" , string1[position]
                  print "block of code \n"
                  print errorcheck[i-3]
                  print errorcheck[i-2]
                  print errorcheck[i]
                  print "Solution :\n"
    else:
      continue
answered Jan 2, 2019 by Omkar
• 69,220 points

Related Questions In Python

+5 votes
1 answer

Print the odd and even factors of a number in Python

x = int(input("Enter any number \n")) print("The factors ...READ MORE

answered Nov 19, 2018 in Python by Nabarupa
17,571 views
+1 vote
1 answer
0 votes
1 answer

How to find the value of a row in a csv file in python and print column and row of that value ?

Hello @Khanhh , Use panda to find the value of ...READ MORE

answered Oct 15, 2020 in Python by Niroj
• 82,840 points
6,777 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,441 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,078 views
0 votes
3 answers

Python program to print occurrence of character in string

Try below code string = input("Enter a string: ...READ MORE

answered May 28, 2020 in Python by Anubhav
• 300 points
30,769 views
+4 votes
3 answers

Write a for loop that prints all elements of a list and their position in the list. a = [4,7,3,2,5,9]

Try using this question by list comprehension: a=[4,7,3,2,5,9] print([x for ...READ MORE

answered Dec 9, 2019 in Python by vinaykumar
• 200 points
34,856 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP