How to split logs into three colums?

I have managed so far to read my Kubernetes pod logs into lines with Python

with open('2215.log','r') as f :
    for line in f.readlines():
             print (line)

Got this output

2023-08-24T12:19:00.536572476+01:00 stderr F Usage:

2023-08-24T12:19:00.536602997+01:00 stderr F    [flags]

2023-08-24T12:19:00.53661012+01:00 stderr F 

2023-08-24T12:19:00.536616965+01:00 stderr F Metrics server flags:

2023-08-24T12:19:00.536623251+01:00 stderr F 

2023-08-24T12:19:00.536631213+01:00 stderr F       --kubeconfig string            The path to the kubeconfig used to connect to the Kubernetes API server and the Kubelets (defaults to in-cluster config)

2023-08-24T12:19:00.536639663+01:00 stderr F       --metric-resolution duration   The resolution at which metrics-server will retain metrics, must set value at least 10s. (default 1m0s)

2023-08-24T12:19:00.536648184+01:00 stderr F       --version                      Show version

2023-08-24T12:19:00.536653981+01:00 stderr F 

2023-08-24T12:19:00.536660756+01:00 stderr F Kubelet client flags:

I want to separate them into

TIMESTAMP+'stderr F'+ the rest

How to do that?

>Solution :

split takes a second argument to define the maxsplits, i.e.:

with open('2215.log','r') as f :
    for line in f.readlines():
        print(line.split(' ', 2))

Will give you 2 splits (therefore give you 3 parts). Note you have to explicitly define the separator too when using this.

Leave a Reply