Linux 是否可以结合使用 tail 和 grep?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14124190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:10:34  来源:igfitidea点击:

Is it possible to use tail and grep in combination?

linuxgreppipelinetail

提问by Pawan

I am trying to tail a user in production log.

我试图在生产日志中跟踪用户。

Is it possible to use

是否可以使用

tail -f grep "username"

采纳答案by Ed Heal

Yes - You use pipe. i.e.

是的 - 您使用管道。IE

tail -f <some filename> | grep 'username'

回答by nakib

Yes, you can just use a pipe

是的,你可以只使用管道

tail -f fileName | grep username

tail -f 文件名 | 用户名

回答by user3211496

factually I have found it to be more efficient to use: grep username filename | tail

事实上,我发现使用它更有效: grep username filename | 尾巴

回答by Andy Lester

The ackcommand, which is a grep-like text finder, has a --passthruflag that is designed specifically for this.

ack命令是一个类似 grep 的文本查找器,有一个--passthru专门为此设计的标志。

Since ackautomatically color codes matches for you, you can use it to search the output of a tailed log file, and highlight the matches, but alsosee the lines that don't match.

由于ack自动为您匹配颜色代码,您可以使用它来搜索尾日志文件的输出,并突出显示匹配项,但也可以查看不匹配的行。

tail -f error.log | ack --passthru whatever

All the lines of the tailed log will show up, but the matches will be highlighted.

尾部日志的所有行都将显示出来,但匹配项将突出显示。

ack is at http://beyondgrep.com/

ack 在http://beyondgrep.com/