管道尾部输出虽然用grep两次 [英] Piping tail output though grep twice

查看:133
本文介绍了管道尾部输出虽然用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"

将不会显示任何输出。

won't show any output.

推荐答案

我相信这里的问题是,第一个是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的:

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 - 什么是缓冲或者说,为什么我的命令行中不产生输出:尾-f日志文件| grep的'富巴| 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天全站免登陆