I wonder how can I search for a certain pattern in file, but only AFTER another pattern appear and stop searching when it encounters the third pattern?
For example, consider the following piece of text:
ServiceMsg : X
Printing BlockList :
Block number: 1
ServiceMsg : Y
ServiceType : GMAP_REQ (1)
Application Context:
Version : GSM_VERSION_3 (3)
Block number: 2
ServiceMsg : Z
Here I want to search for the ServiceMsg pattern, however only after Block number appears and do not continue the search after additional Block number. Thus the result must be only ServiceMsg : Y.
EDIT:
These blocks come in threes:
.
Block number: 1
.
.
Block number: 2
.
.
Block number: 3
.
…And there it goes again
I want to be able to retrieve the occurrences of ServiceMsg between Block number: 1 and Block number: 2and also between Block number: 2 and Block number: 3.
Thanks!
update after question rewriting:
Searches for ServiceMsg after Block number patterns (g)lobally. If it finds a ServiceMsg, it reads the (n)ext line, since the last line is not included. Then matches
ServiceMsg, (p)rints it (g)lobally (not just once). -n prevents printing in general.Test file:
Output: