#How can I only select only the time from a line with lot of information

1 messages · Page 1 of 1 (latest)

long bay
#

I have a server which logs everything into a text file. I have attached a sample image below. From the lines I only want the last line's data in which from LOG : Network , 1675939666804> 16,803,449> [09-02-23 18:47:46.804] > ZNet: SZombienet -> SSteamSDK: LogOff this it only selects 18:47. How can I do it? I also want it to check the file every 10 seconds. Every 10 seconds it should be able to check the latest line and extract the time from it.

Could someone kindly guide me how I will be able to pull this off.
Thank you!

vague arrow
#

You need to open the file, loop over the lines and get the line if it's the one you want.

with open('file.txt', 'r') as f
    for line_nr, line in enumerate(f):
        if line_nr == 5:  # Check for line 5
            # Do whatever you want with the line
            break

Or if you always want the last line, read all lines and pick the last one.

with open('file.txt', 'r') as f
    lines = f.readlines()
    last_line = lines[-1]  # index '-1' is the last element in a list
#

Keep in mind that readlines will read every line into memory. So if your file is extremely large, it may cause issues. In that case I would look at splitting your logs up into multiple files. You could for instance store your logs in a file named after the current date.

#

You could also go for the f.seek approach. Which is faster and more memory efficient, but it's a lot more complex and unnecessary if your files aren't super large.

civic sleet
#

You could use regex to get the text inside the [ ] and regex groups to get 18:47 part (ofc not hard-coded) from within that

long bay
#

Okay understood

#

works as intended

#

Thank you gentlemen!

vague arrow
#

.solved

flat mapleBOT
#

Done with your help thread?

Please close your own help thread by using </close:1009144375709814897> with @dull urchin.