通过 grep 两次管道尾部输出 [英] Piping tail output though grep twice

查看:49
本文介绍了通过 grep 两次管道尾部输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用典型的 Apache 访问日志,您可以运行:

Going with a typical Apache access log, you can run:

tail -f access_log | grep "127.0.0.1"

它只会向您显示指定 IP 地址的日志(创建时).

Which will only show you the logs (as they are created) for the specified IP address.

但是为什么当你第二次通过 grep 管道它时会失败,以进一步限制结果?

But why does this fail when you pipe it though grep a second time, to further limit the results?

例如,.css"的简单排除:

For example, a simple exclude for ".css":

tail -f access_log | grep "127.0.0.1" | grep -v ".css"

不会显示任何输出.

推荐答案

我相信这里的问题是第一个 grep 正在缓冲输出,这意味着第二个 grep 在缓冲区被刷新之前不会看到它.

I believe the problem here is that the first grep is buffering the output which means the second grep won't see it until the buffer is flushed.

尝试在您的第一个 grep 中添加 --line-buffered 选项:

Try adding the --line-buffered option on your first grep:

tail -f access_log | grep --line-buffered "127.0.0.1" | grep -v ".css"

有关详细信息,请参阅 "BashFAQ/009 -- 什么是缓冲?或者,为什么我的命令行没有输出:tail -f logfile | grep 'foo bar' | awk ..."

For more info, see "BashFAQ/009 -- What is buffering? Or, why does my command line produce no output: tail -f logfile | grep 'foo bar' | awk ..."

这篇关于通过 grep 两次管道尾部输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆