fatbij
fatbij

Reputation: 7

Charting values using python

I have a log file which shows data sent in the below format -

2019-10-17T00:00:02|Connection(10.0.0.89 :0 ) r=0 s=1024 d=0 t=0 q=0 # connected 2019-10-17T00:00:02|McSend (229.0.0.70 :20001) b=1635807
f=2104 d=0 t=0

There will be multiple lines per file

How can I graph the b=value against the time (near the beginning on the line) but only from the McSend lines

Thanks

Upvotes: 1

Views: 51

Answers (2)

Vilius Klakauskas
Vilius Klakauskas

Reputation: 128

If you're not familiar with regular expressions - python regex documentation is a good place to start.

The simplest regex you probably need is r"^(\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d)\|.*McSend.*+b=(\d+)"

First group will allow you compare the timestamp and the second will give the value.

import re

pattern =  r"^(\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d)\|.+McSend.+b=(\d+)"
#result is a list of tuples containing the time stamp and the value for b
result = re.findall(pattern, some_input) 

Upvotes: 1

drheinrich940
drheinrich940

Reputation: 143

You should read your file line by lines. Then scan for each line if it contains 'McSend'. If it does then retrieve the desired data.

You could do something like this :


b_values = []
dates = []

## Lets open the file and read it line by line
with open(filepath) as f:
    for line in f:
## If the line contains McSend
        if 'McSend' in line :
## We split the line by spaces ( split() with no arguments does so )
            splited_line = line.split()
## First string chunk contains the header where the date is located
            header = splited_line[0]
## Then retrieve the b value 
            for val in splited_line :
                if val.startswith('b=') :
                    b_value = val.split("=",1)[1] 
## Now you can add the value to arrays and then plot what you neet
                    b_values.append(b_value)
                    dates.append(header.split("|",1)[0] 
## Do your plot

Upvotes: 0

Related Questions