Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

linux - 如何“ grep”连续流?(How to 'grep' a continuous stream?)

Is that possible to use grep on a continuous stream?

(可以在连续流上使用grep吗?)

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.

(我的意思是有点tail -f <file>命令,但是在输出中使用grep以便仅保留我感兴趣的行。)

I've tried tail -f <file> | grep pattern

(我已经试过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.

(tail -f <file> | grep pattern但似乎只能在tail完成后才执行grep ,也就是说永远不会执行。)

  ask by Matthieu Napoli translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(使用BSD grep(FreeBSD,Mac OS X等)时,请打开grep的行缓冲模式。)

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).

(您无需为GNU grep(几乎在任何Linux上使用)执行此操作,因为它会默认刷新(对于其他类似Unix的版本,如SmartOS,AIX或QNX,则为YMMV)。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...