Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tail -f + grep? [duplicate]

Tags:

Tail has the following options:

-f      The -f option causes tail to not stop when end of file is reached, but rather to wait for additional data to be appended to the              input.  The -f option is ignored if the standard input is a pipe, but not if it is a FIFO. 

I'd like to only grep for something in the tail output.

tail -f <FILE> | grep <SOMETHING>  

Issue is it only run grep once and is done. No other output happens. How can I make grep run correctly with the -f?

like image 944
Sten Kin Avatar asked Apr 30 '14 18:04

Sten Kin


People also ask

How do I print 10 lines after grep?

You can use grep with -A n option to print N lines after matching lines. Using -B n option you can print N lines before matching lines. Using -C n option you can print N lines before and after matching lines.

How do I print above and below lines in grep?

It is possible to print a line above or below (or both) a line having a pattern using grep by using -A , -B or -C flags with num value. Here num denotes the number of additional lines to be printed which is just above or below the matched line.


1 Answers

You will find another SO Question helpful: How to 'grep' a continuous stream?

Turn on grep's line buffering mode.

tail -f file | grep --line-buffered my_pattern 
like image 115
Sunny Patel Avatar answered Sep 30 '22 03:09

Sunny Patel