Python Forum

Full Version: read logfile between two specific strings
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey Community,

i am trying to read a log file and want to analyze all the parts between two substrings which appear always before and after the interesting part.

example:

xxx event start ffff
interest1
interest2
interest3
sssss event end ttttt
uninteresting
unintersting
vvvvv event start lllll
interest4
interest5
interest6
yyyy event end oooo
...

I would like to have each "interesting part" as a list, which contains each interesting line of this part.
If anyone could help me it would be great.

Thanks a lot and happy new year!
from io import StringIO

text = """
xxx event start ffff
interest1
interest2
interest3
sssss event end ttttt
uninteresting
unintersting
vvvvv event start lllll
interest4
interest5
interest6
yyyy event end oooo"""

fake_file = StringIO(text)


for line in fake_file:
    if line.startswith("interest"):
        print(line, end="")
With a real file:
with open("your_log.txt") as fd:
    for line in fd:
        if line.startswith("interest"):
            print(line, end="")
Instead of printing the line, you can collect them in a list.
Regex could also solve this problem, but often it's easier to use the str methods.
Thank you DeaD_EyE for your quick reply. I just realized that i didn't make myself completely clear. The lines of interest contain whatever information so they dont follow the same sructure or have a specific word in them.
So basically i want my code to say: find text between the first "start" and "end", then the second "start" and "end"...
(The example lines which contain "start" and "end" are only longer to be easier found in the text.)

An updated example text would be:

text = """
somethingsa
fasssdfsdfaasdsfs start asdfa
dafafgfg
sfjhfdasga
sfhgaf
safgdfdFAF
hsgshsfhsshgsghss end sfghsh
jfdgdfggd
fhgdgkj
dfgfasdfdasfdffag start asdfdasf
afgghsfhgag
sdfa
safdssagfg
sadfgasfdffgafgh end adsfg
kjdhjgjd
sdhgfdfsdhdj"""

Thanks so much for your help!!
You need to be able to detect event start and event end lines. At the moment we don't know what xxx, fff, sssss, etc. are. But of course you can always check if event start or event end is present in the line and collect lines in between

Now, your example is just start and end
Hey buran,
oh yes, i changed the example, because i wanted to clarify that the start and the end don't partially consist of the same string.

If you could help me with this code, how to collect the lines between the ones containing start and end i would be really happy, because i'm really struggling here.
one way

text = """
xxx event start ffff
interest1
interest2
interest3
sssss event end ttttt
uninteresting
unintersting
vvvvv event start lllll
interest4
interest5
interest6
yyyy event end oooo"""


def get_lines(f):
    flag = False
    for line in f:
        if 'start' in line:
            flag = True
        elif 'end' in line:
            flag = False
        else:
            if flag:
                yield line

# with open('yourfile') as f:
    # for line in get_lines(f):
    #     print(line)
    
for line in get_lines(text.splitlines()):
    print(line)
    
Great, it works! Thanks very much for the support. Learned a lot from this.