Skip to content
Advertisement

python: print a single column using field separator

I am beginner with python. From a log, I want with a python to extract only the hostname that are located in the middle of each line line ( between “command_wrappers INFO:” and “: pg_receivewal: switched to timeline”) in order to lauch a command to each of thoses servers.

Here are the lines of the log:

2020-07-16 10:13:03,065 [105879] barman.command_wrappers INFO: epgbd-b2bintraextranet: pg_receivewal: switched to timeline 13 at 1F/BC062600
2020-07-16 10:13:03,185 [105877] barman.command_wrappers INFO: epgbd-asteriaconnect: pg_receivewal: switched to timeline 3 at 8/C0066708
2020-07-16 10:13:03,341 [105881] barman.command_wrappers INFO: epgbd-comel: pg_receivewal: switched to timeline 5 at 50/6D04FB30
2020-07-16 10:13:03,352 [105884] barman.command_wrappers INFO: epgbd-dommages-ouvrages: pg_receivewal: switched to timeline 4 at 4C/2605C388
2020-07-16 10:13:03,726 [105888] barman.command_wrappers INFO: epgbd-efs: pg_receivewal: switched to timeline 3 at 50/8C067640

Here is the result that I am looking for:

epgbd-b2bintraextranet
epgbd-asteriaconnect
epgbd-comel
epgbd-dommages-ouvrages
epgbd-efs

Is there a simple way to get such result ?

Advertisement

Answer

In general you will usually use a Regex to solve this kind of stuff, but in your case. You can simply use split() method.

If your case it would look like:

>>mystr = "2020-07-16 10:13:03,065 [105879] barman.command_wrappers INFO: epgbd-b2bintraextranet: pg_receivewal: switched to timeline 13 at 1F/BC062600"
>>mystr.split(":")[3].strip()
>>'epgbd-b2bintraextranet'

If the logs looks exactly as the example…

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement