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 in Python by slayer
• 29,050 points
59 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 by Omkar
• 67,120 points

Related Questions In Python

+1 vote
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
230 views
+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,420 points
1,149 views
+1 vote
4 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
169 views
0 votes
1 answer

How can I compare the content of two files in Python?

Assuming that your file unique.txt just contains ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,710 points
57 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 133 views
+4 votes
6 answers
+1 vote
2 answers

Python program to print occurrence of character in string

Try the below code inputString = input("Enter a ...READ MORE

answered Feb 26 in Python by Anubhav
• 160 points
223 views
0 votes
2 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]

a = [4,7,3,2,5,9] b = enumerate(a) for i in ...READ MORE

answered 6 days ago in Python by Kei Tee
42 views