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 StartPatterns with missing EndPattern, I'd only like lines from the last StartPattern to the EndPattern.
For a StartPattern which reaches the EOF without an EndPattern, I'd like everything up to the EOF, followed by inputting a string to warn that EOF was reached.
Best Answer
You can always do:
If your system doesn't have GNU
tac
, you may be able to usetail -r
instead.You can also do it like:
But that means reading the whole file.
Note that it may give different results if there's another
StartPattern
in between aStartPattern
and the nextEndPattern
or if the lastStartPattern
does not have an endingEndPattern
or if there are lines matching bothStartPattern
andEndPattern
.Would make it behave more like the
tac+sed+tac
approach (except for the unclosed trailingStartPattern
case).That last one seems to be the closest to your edited requirements. To add the warning would simply be:
To avoid reading the whole file:
Portability note: for
/dev/stderr
, you need either a system with such a special file (beware that on Linux if stderr is open on a seekable file that will write the text at the beginning of the file instead of the current position within the file) or anawk
implementation that emulates it likegawk
,mawk
or busyboxawk
(those work around the Linux issue mentioned above).On other systems, you can replace
print ... > "/dev/stderr"
withprint ... | "cat>&2"
.