Python Forum
capture next block of text after finding error in file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
capture next block of text after finding error in file
#7
error.log
Output:
2019-11-18 01:46:47:INFO:3496839:/var/logs/apc/ : no errors detected. 2019-11-18 01:46:47:INFO:3496839:/var/logs/xyz/ : no errors detected. 2019-11-18 01:46:47:ERROR:3496839:check reported errors in the /var/logs/jkl/ database. These should be rechecked to verify if the errors are accurate. 2019-11-18 01:46:47:ERROR:3496839:check reported errors in the /var/logs/jkl/spam database. These should be rechecked to verify if the errors are accurate.
using just str methods
log_file = 'error.log'

with open(log_file) as lf:
    for line in lf:
        log_date_hour, log_minute, log_seconds, log_type, some_code, info, *rest = line.split(':')
        if log_type == 'ERROR':
            print(info.split(' ')[5])
Output:
/var/logs/jkl/ /var/logs/jkl/spam
using regex

import re
regex = re.compile(r'reported errors in the (?P<path>\S*)', flags=re.MULTILINE)
with open(log_file) as lf:
    logs = lf.read()

paths = regex.findall(logs)
print(paths)
Output:
['/var/logs/jkl/', '/var/logs/jkl/spam']
if file is huge it may be better to read it line by line (like in first example and use regex to parse the line). maybe it's possible to make better regex pattern though

also both snippets will fail if there is space in the path. if you expect these you may have to adjust the code accordingly
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: capture next block of text after finding error in file - by buran - Nov-27-2019, 07:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error is finding mean of a list PythonBoy 4 913 Sep-11-2023, 02:38 PM
Last Post: PythonBoy
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,124 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  How to write in text file - indented block Joni_Engr 4 6,460 Jul-18-2022, 09:09 AM
Last Post: Hathemand
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,673 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,983 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Screen capture opencv - grab error Kalman15 1 1,606 Jan-27-2022, 12:22 PM
Last Post: buran
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,354 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Unable to capture all images of a multipage TIFF file in a merge bendersbender 0 2,217 Nov-19-2020, 03:09 PM
Last Post: bendersbender
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,400 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  cx_Oracle.DatabaseError: Error while trying to retrieve text from error ORA-01804 rajeshparadker 0 8,643 Nov-12-2020, 07:34 PM
Last Post: rajeshparadker

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020