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:
JavaScript
x
6
1
2020-07-16 10:13:03,065 [105879] barman.command_wrappers INFO: epgbd-b2bintraextranet: pg_receivewal: switched to timeline 13 at 1F/BC062600
2
2020-07-16 10:13:03,185 [105877] barman.command_wrappers INFO: epgbd-asteriaconnect: pg_receivewal: switched to timeline 3 at 8/C0066708
3
2020-07-16 10:13:03,341 [105881] barman.command_wrappers INFO: epgbd-comel: pg_receivewal: switched to timeline 5 at 50/6D04FB30
4
2020-07-16 10:13:03,352 [105884] barman.command_wrappers INFO: epgbd-dommages-ouvrages: pg_receivewal: switched to timeline 4 at 4C/2605C388
5
2020-07-16 10:13:03,726 [105888] barman.command_wrappers INFO: epgbd-efs: pg_receivewal: switched to timeline 3 at 50/8C067640
6
Here is the result that I am looking for:
JavaScript
1
6
1
epgbd-b2bintraextranet
2
epgbd-asteriaconnect
3
epgbd-comel
4
epgbd-dommages-ouvrages
5
epgbd-efs
6
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:
JavaScript
1
4
1
>>mystr = "2020-07-16 10:13:03,065 [105879] barman.command_wrappers INFO: epgbd-b2bintraextranet: pg_receivewal: switched to timeline 13 at 1F/BC062600"
2
>>mystr.split(":")[3].strip()
3
>>'epgbd-b2bintraextranet'
4
If the logs looks exactly as the example…