需要有关另一个 grep 命令输出的 grep 帮助
我有一个文件 test.log。非常大的日志文件。它有不同级别的日志记录。例如,trace
、debug
、info
、warning
和 error
。
显然,trace
级别的消息只是高速发送的垃圾邮件。我想查看没有 trace
级别日志的所有消息。
所以我这样做了:
cat test.log | grep -v "trace"
效果很好。
现在我想根据某个关键字 keyword1
过滤剩余的消息。
所以我这样做了:
cat test.log | grep -v "trace" | grep "keyword1"
效果很好。
现在我想连续获得相同的输出,我想用 tail -f
替换 cat
。
tail -f test.log | grep -v "trace" | grep "keyword1"
但这是行不通的。我根本没有任何输出。
我做错了什么?我怎样才能得到我想要的过滤“尾部和尾部”?跟随'输出。
感谢您的帮助。
(顺便说一句,我正在使用 cygwin...如果这有什么关系的话)
I have a file test.log. Very big log file. It has different levels of logging. For eg, trace
, debug
, info
, warning
and error
.
Obviously trace
level messages are just spamming at high speeds. I wanted to see all the messages without trace
level logs.
So I did this:
cat test.log | grep -v "trace"
Works good.
Now I want to filter the remaining messages based on a certain keyword keyword1
.
So I did this:
cat test.log | grep -v "trace" | grep "keyword1"
Works good.
Now I want to get that same output on a continual basis, I thought of replacing cat
with tail -f
.
tail -f test.log | grep -v "trace" | grep "keyword1"
But this does not work. I get no output at all.
What am I doing wrong? And how can I get my desired filtered 'tail & follow' output.
Thanks for the help.
(btw, I am using cygwin... if that matters in any way)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您遇到了缓冲问题:出于性能原因,
grep
将在其缓冲区中保留相当多的输出,并一次性输出整个块。这意味着当从输入中读取一行时,一旦读取(并允许通过)更多行,grep 就会将其发送到 stdout - 这在处理日志时可能需要相当长的时间使用tail -f
读入的文件。许多 grep 变体都可以切换到打开行缓冲模式,该模式将单独输出每一行 - 会造成一些性能损失。例如
GNU grep
有一个--line-buffered
选项来达到这个效果。只需将该选项(或适合您的
grep
版本的选项)添加到所有grep
调用中,只要将匹配的类似内容添加到日志中,您就会看到一些输出文件。You are encountering buffering issues: for performance reasons
grep
will keep quite a bit of output in its buffer and will output the whole chunk in one go. That means that when a line is read from the input,grep
will send it to stdout once it has read (and let through) even more lines - which can be quite some time later when dealing with a log file that is being read-in usingtail -f
.Many
grep
variants have a switch to turn-on line-buffered mode, which will output each line on its own - with some performance loss. For exampleGNU grep
has a--line-buffered
option to achieve this effect.Just add that option (or the appropriate one for your version of
grep
) to all yourgrep
invocations and you see some output as soon as a matching like is added to the log file.您的代码工作正常,您只是遇到缓冲延迟。因此,您将看到很长一段时间没有任何内容,然后是短暂的大量文本爆发,然后是另一次等待。阅读 http://perl.plover.com/FAQs/Buffering.html 获取解释正在发生的事情。
Your code is working fine, you're just encountering buffering delays. So you will see a long period of nothing, followed by a short burst of lots of text, followed by another wait. Read http://perl.plover.com/FAQs/Buffering.html for an explanation of what is going on.
tail -f
跟随文件的“新传入行”。周期性输出永远不会到达管道grep
命令(至少不会,直到tail
终止)。要定期“跟踪”这些日志文件中的更改,您可能需要使用
watch
:这将每秒 (
-n 1
) 更新日志文件的最后 20 行日志档案。tail -f
follows the "new incoming lines" of a file. The periodic output will never reach the pipedgrep
commands (at least not, untiltail
is terminated).To periodically "follow" changes in those log files, you might want to use
watch
instead:This'll update every second (
-n 1
) the last 20 lines of the log file.