0

I need to open a log file, and print out specific lines that are in between two different patterns.

Beginning = pattern1
Ending = pattern2

This is what I have so far:

       def SeeIfDateTimeIsFound():
           mylist = []
           for line in input_data:
               if CTFBeginA in line:  # Or whatever test is needed
                   if re.search(stringA, line) and re.search(stringB, line):
                       mylist.append((line.strip()))
                       break
           for line in input_data:  # This keeps reading the file
                   if CTFEndinA in line.strip():
                       nextline = (next(input_data, '').strip())
                       if CTFEndin not in nextline:
                           break
                       else:
                           if re.search(stringA, line) and re.search(stringB, line):
                               mylist.append((line.strip()))
                           if re.search(stringA, line) and re.search(stringB, line):
                               mylist.append((line.strip()))
           return mylist

What I'm trying to do is basically the equivalent of this awk code:

awk '/Beginning/,/Ending/' logfile

One of the problems i have with the python code above is, it only grabs the first line it finds that contains Ending pattern. If there are multiple lines containing Ending, i want the python code to grab every one of them and only stop grabbing them when it has grabbed the last one.

swenson
  • 67
  • 6

1 Answers1

0

You could do something on these line:

print_swt = 0
with open(logfile,'r') as f:
     for line in f:
         if Beginning in line:
            print_swt =1
         if Ending in line:
            print_swt = 0

         if print_swt:
            print line

Note: This may not be the most pythonic way of doing what you want to do.

Ash Sharma
  • 470
  • 3
  • 18
  • i have updated my original post. the previous code i had there was one of the many attempts i was making to fix this. please review. – swenson Jun 28 '18 at 13:14