尾巴-f + grep?

时间:2014-04-30 18:39:48

标签: linux bash unix grep tail

Tail有以下选择:

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

我只想在尾部输出中只​​查看something

tail -f <FILE> | grep <SOMETHING> 

问题是它只运行一次grep并完成。没有其他输出发生。如何使用-f正确运行grep?

2 个答案:

答案 0 :(得分:37)

您会发现另一个有用的问题:How to 'grep' a continuous stream?

启用grep的线路缓冲模式。

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

答案 1 :(得分:7)

如果这是一个日志文件,它可能会被轮换。然后它将停止提供数据。
如果文件被旋转,这不会停止。

tail --follow=name /var/log/syslog | grep "some data"