How to 'grep' a continuous stream?

Is that possible to use grep on a continuous stream?

What I mean is sort of a tail -f <file> command, but with grep on the output in order to keep only the lines that interest me.

I've tried tail -f <file> | grep pattern tail -f <file> | grep pattern but it seems that grep can only be executed once tail finishes, that is to say never.


Turn on grep 's line buffering mode when using BSD grep (FreeBSD, Mac OS X etc.)

tail -f file | grep --line-buffered my_pattern

You don't need to do this for GNU grep (used on pretty much any Linux) as it will flush by default (YMMV for other Unix-likes such as SmartOS, AIX or QNX).


I use the tail -f <file> | grep <pattern> tail -f <file> | grep <pattern> all the time.

It will wait till grep flushes, not till it finishes (I'm using Ubuntu).


I think that your problem is that grep uses some output buffering. Try

tail -f file | stdbuf -o0 grep my_pattern

it will set output buffering mode of grep to unbuffered.

链接地址: http://www.djcxy.com/p/74312.html

上一篇: 以编程方式设置UIButton标题UILabel字体大小

下一篇: 如何'grep'连续的流?