需要有关另一个 grep 命令输出的 grep 帮助

发布于 2024-10-15 22:15:15 字数 760 浏览 2 评论 0原文

我有一个文件 test.log。非常大的日志文件。它有不同级别的日志记录。例如,tracedebuginfowarningerror

显然,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

波浪屿的海角声 2024-10-22 22:15:15

您遇到了缓冲问题:出于性能原因,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 using tail -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 example GNU 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 your grep invocations and you see some output as soon as a matching like is added to the log file.

入怼 2024-10-22 22:15:15

您的代码工作正常,您只是遇到缓冲延迟。因此,您将看到很长一段时间没有任何内容,然后是短暂的大量文本爆发,然后是另一次等待。阅读 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.

小猫一只 2024-10-22 22:15:15

tail -f 跟随文件的“新传入行”。周期性输出永远不会到达管道 grep 命令(至少不会,直到 tail 终止)。

要定期“跟踪”这些日志文件中的更改,您可能需要使用 watch

watch -n 1 -- 'tail -n 20 test.log | grep -v trace | grep keyword1'

这将每秒 (-n 1) 更新日志文件的最后 20 行日志档案。

tail -f follows the "new incoming lines" of a file. The periodic output will never reach the piped grep commands (at least not, until tail is terminated).

To periodically "follow" changes in those log files, you might want to use watch instead:

watch -n 1 -- 'tail -n 20 test.log | grep -v trace | grep keyword1'

This'll update every second (-n 1) the last 20 lines of the log file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文