Is there a way to grep
a log and find text between the log entry delimeters?
Our log file separates the line entry with characters "-------
"
So when I search the text word I want all the lines before and after the delimeters in the log.
Sample log
------------------------------------------------------------------------
r132279 | USERID | 2014-04-30 12:59:09 -0700 (Wed, 30 Apr 2014) | 3 lines
Removed unused "Calculated Fields" column entry.
Jira ID: JIRA-977
------------------------------------------------------------------------
In the above i would Grep the word Fields but want all the lines between the "----
" lines
Best Answer
If you know how big the record is, then you can output additional lines of context before (
-B
) and after (-A
) the matching line e.g.or for context both before and after the match line
As far as I know, the only way to do a true multiline match (rather than a single line match plus context) in GNU grep is by using the PCRE regex mode (
-P
) with the-z
flag to prevent breaking on newlines. For example, you could trywhich does a non-greedy match of the string
Fields
surrounded by any characters OR newlines, provided it is bookended by the newline-hyphens-newline delimiters. An equivalent expression in pcregrep isAnother option for this kind of record-structured data is awk: in particular, GNU awk allows a regular expression to be used for the internal record separator RS e.g.