[英]Multiple grep piping (include+exclude) results in not showing anything
目前,我正在尝试tail
日志,但仅显示包含某些关键字的行。 目前我正在使用
tail -F file.log | grep -ie 'error\|fatal\|exception\|shutdown\|started'
并且我得到了预期的结果:(例如)
10:22 This is an error
10:23 RuntimeException: uncaught problem
我还想排除包含<DATATAG>
,即使关键字已滑入其中也是如此,因为它包含许多使我的日志<DATATAG>
的二进制数据。 然后,我尝试向管道中添加不包含标记的另一个grep:
tail -F file.log | grep -ie 'error\|fatal\|exception\|shutdown\|started' | grep -vF '<DATATAG>'
但是,这次没有出现任何行,甚至没有出现'error'/'exception'但没有<DATATAG>
。 当我尝试单独使用排除grep时:
tail -F file.log | grep -vF '<DATATAG>'
出现所有行,包括具有“错误” /“异常”的行。
难道我做错了什么?
您的问题是缓冲之一。 grep
是一个棘手的工具。 从手册页:
By default, output is line buffered when standard output is a terminal and block buffered otherwise.
在您的示例中,第一个grep
在块级别进行缓冲,因此暂时不会将输出转到第二个grep
。 解决方案是使用--line-buffered
选项如下所示:
tail -F file.log | grep --line-buffered -ie 'error\|fatal\|exception\|shutdown\|started' | grep -vF '<DATATAG>'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.