我正在过滤 Apache 日志,但连接多个过滤器时没有输出。试图将问题减少到一个最小的例子,grep 似乎无法将输出转发到管道
$ tail -F access_log | grep test
10.108.57.6 - - [03/Nov/2017:12:35:55 +0000] "GET /test HTTP/1.1" 404 202 "-" "curl/7.11.0 (i686-pc-linux-gnu) libcurl/7.11.0 OpenSSL/1.0.2d ipv6 zlib/1.1.3" 161
$ tail -F access_log | grep test | grep test
# nothing
$ tail -F access_log | grep test | sed 's/test/asd/g'
# nothing
$ tail -F access_log | sed 's/test/asd/g' | grep asd
10.108.57.6 - - [03/Nov/2017:12:44:58 +0000] "GET /asd HTTP/1.1" 404 202 "-" "curl/7.11.0 (i686-pc-linux-gnu) libcurl/7.11.0 OpenSSL/1.0.2d ipv6 zlib/1.1.3" 143
有光吗?
我使用的是 Centos 7.2,grep 2.20
最佳答案
用于 grep 流 (tail -f
)
使用 grep --line-buffered
几乎总是比使用 grep
来自官方文档:
--line-buffered Use line buffering on output. This can cause a performance penalty.
换句话说:每次 grep 获取输入时都写入输出,并且不等待填充 grep 缓冲区
关于bash - grep 后无法通过管道输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47096347/