Python Forum
read logfile between two specific strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
read logfile between two specific strings
#1
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!
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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!!
Reply
#4
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
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
#5
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.
Reply
#6
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)
    
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
#7
Great, it works! Thanks very much for the support. Learned a lot from this.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read module/class from list of strings? popular_dog 1 467 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Trying to understand strings and lists of strings Konstantin23 2 756 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,754 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  [Solved] Trying to read specific lines from a file Laplace12 7 3,523 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  Read strings and numbers in columns from a file suvadip 4 2,869 Aug-11-2020, 09:37 PM
Last Post: suvadip
  Read Multiples Text Files get specific lines based criteria zinho 5 3,108 May-19-2020, 12:30 PM
Last Post: zinho
  Delete specific lines contain specific words mannyi 2 4,117 Nov-04-2019, 04:50 PM
Last Post: mannyi
  Distilling strings using .writelines() and .read() tedie 1 2,009 Jun-24-2019, 09:56 PM
Last Post: micseydel
  Finding multiple strings between the two same strings Slither 1 2,511 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  How to read what's written in THIS specific page ? duongtn34 0 1,806 Sep-19-2018, 04:50 AM
Last Post: duongtn34

Forum Jump:

User Panel Messages

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