I'd like to tail a log, find some text and only output the the line found plus the next line, is it possible?
THE QUICK
BROWN FOX
JUMPED OVER
THE LAZY
DOG'S BACK
Something like: tail -f | grep BROWN FOX
output would be:
BROWN FOX
JUMPED OVER
tail -<num1> filename.log | grep "search_string" -A num2
Example file: a.log
10 20
10 12
11 14
tail -2 a.log | grep "10" -A 1 # tail -2 indicates two lines from the end of the file
Output
10 12
11 14
Grep options
Use -n
option in grep to print line number
Use -C num
option to get leading and trailing context(adjacent lines to the matching one)
Use -A num
option to get trailing num of lines
Use -B num
option to get leading num of lines
tail -f log_file_name | grep --line-buffered -A1 BROWN FOX
--line-buffered
causes grep to respond more promptly to the piped input (very useful if the log file grows slowly)
-A1
asks it to also display one line after each match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With