So here is my requirement.
I am tailing a log file and grepping on it.
I want to get some context on every grep result..
But the context should be "till a pattern is matched" and not the number of lines (which is what grep -A/-B/-C
offer).
For example
Say here is my log..
[l]
is prefixed to every log line. Also there will be prefix of [logTypeA]
or [logTypeB]
[l][logTypeA] - Log line 1
[l][logTypeB] - Log line 2
[l][logTypeA] - Log line 3
....
Random data about Log line 3
....
[l][logTypeB] - Log line 4
Now my if my tail
command was tail -f log_file.log | grep "[logTypeA]"
,
I'd get an output of
[l][logTypeA] - Log line 1
[l][logTypeA] - Log line 3
But I need contextual information for my grep result, and that context is NOT some number of lines, but rather till a particular pattern is matched (in this case [l])..
In the example I want my grep result to be
[l][logTypeA] - Log line 1
[l][logTypeA] - Log line 3
....
Random data about Log line 3
....
From here (How to show lines after each grep match until other specific match?), I tried sed
command on my tail
like
tail -f log_file.log | sed '/\[logTypeA\]/,/\[l\]/p'
But that doesn't seem to work.
Any ideas?
tail -f
gives buffered output and can not play with "past" and "future" rangesed
script looks like you want to print lines with[l]
and all lines between them ... but without any further conditions, that's simply equivalent to all lines, to my understanding. I guess you want all[l]
lines which match a particular additional pattern and all non-[l]
lines after a match, but without further details, there are too many variables and guesses to write a useful script. It could probably done insed
instead if that's your preference, though Awk seems particularly suitable here.