Skip to content Skip to sidebar Skip to footer

Python Print Between Two Patterns, Including Lines Containing Patterns

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 S

Solution 1:

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.


Post a Comment for "Python Print Between Two Patterns, Including Lines Containing Patterns"