Server Stopped
ABC
DEF
GHI
JKL
Server Started
MNO
PQR
STU
Server Stopped
VWX
YZ
ABC
Server Started
Server Stopped
123
456
789
Server Started
I want to get only this in the output to a log file :
Server Stopped
123
456
789
Server Started
i.e. The text between last Server stopped and Server Started and ignore others.
Can someone please help. I had look into tac but not getting the exact solution.
/Server Stopped/h ==> if line we found 'Server Stopped', we initialize hold buffer with it. /Server Stopped/,/Server Started/{/Server Stopped/b,H;} ==> Between lines 'Server Stopped' and 'Server Started', if the line is 'Server Stopped', we will at the end of the sed command to begin to the next line, else adds the other lines in the hold buffer. ${x,p;} ==> if last line, swap the hold buffer and the current buffer and the current buffer is displayed.
>>> import re
>>> text = '''
... Server Stopped
... ABC
... DEF
... GHI
... JKL
... Server Started
... MNO
... PQR
... STU
... Server Stopped
... VWX
... YZ
... ABC
... Server Started
... Server Stopped
... 123
... 456
... 789
... Server Started
... '''
>>> pattern = '(^Server Stopped\s*\n^(?:\d+\n)+^Server Started)'
>>> for found in re.search(pattern,text,re.M).groups():
... print found
...
Server Stopped
123
456
789
Server Started