I believe this is a 3 step process but please bear with me. I'm currently reading Shell output which is being saved to a file and the output looks like this:
Current Output:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 123.345.789:1234 0.0.0.0:* LISTEN 23044/test
tcp 0 0 0.0.0.0:5915 0.0.0.0:* LISTEN 99800/./serv
tcp 0 0 0.0.0.0:1501 0.0.0.0:* -
I'm trying to access each columns information based on the header value. This is something I was able to do in Powershell but not sure how to achieve it in Python.
Expected Output:
Proto,Recv-Q,Send-Q,Local Address,Foreign Address,State,PID/Program name
tcp,0,0,123.345.789:1234,0.0.0.0:*,LISTEN,23044/test
tcp,0,0,0.0.0.0:5915,0.0.0.0:*,LISTEN,99800/./serv
tcp,0,0,0.0.0.0:1501,0.0.0.0:*,,-
proto = data["Proto"]
for p in proto:
print(p)
Output: tcp tcp tcp
What I've tried?:
Where do I begin.. I've tried Splitting, Replacing and Translate. Also, I did try Regex but couldn't quite figure it out :/
Proto,Recv-Q,Send-Q,Local,Address,,,,,,,,,,,Foreign Address,,,,,,,,,State,,,,,, PID/Program,name
tcp,,,,,,,,0,,,,,,0 123.345.789:1234,,,,,,,,0.0.0.0:*,,,,,,,,,,,,,,,LISTEN,,,,,,23021/java,,,,,,,,
tcp,,,,,,,,0,,,,,,0 0.0.0.0:5915,,,,,,,,,,,,0.0.0.0:*,,,,,,,,,,,,,,,LISTEN,,,,,,99859/./statserv
tcp,,,,,,,,0,,,,,,0 0.0.0.0:1501,,,,,,,,,,,,0.0.0.0:*,,,,,,,,,,,,,,,LISTEN,,,,,,-
Since some of the headers contain a space in between them it's sort of difficult to map the columns accordingly.
Looking for the best way to approach this.
Thank you.