I have a log file which reports on the output of a process, I'd like to extract all lines from between the last occurrence of two patterns.
The patterns will be along the lines of;
Summary process started at <datestring>
and
Summary process finished at <datestring> with return code <num>
There will be several instances of these patterns throughout the file, along with a lot of other information. I'd like to print the only the last occurrence.
I know that I can use:
sed -n '/StartPattern/,/EndPattern/p' FileName
to get lines between the patterns, but not sure how to get the last instance.
sed
or awk
solutions would be fine.
Edit:
I've not been clear at all about the behaviour that I want when multiple StartPatterns
appear with no EndPattern
, or if there's no EndPattern
before the end of file, after detecting a StartPattern
.
- For multiple
StartPattern
s with missingEndPattern
, I'd only like lines from the lastStartPattern
to theEndPattern
. - For a
StartPattern
which reaches theEOF
without anEndPattern
, I'd like everything up to theEOF
, followed by a warning thatEOF
was reached prematurely.