Linux 如何“grep”连续流?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7161821/
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
How to 'grep' a continuous stream?
提问by Matthieu Napoli
Is that possible to use grep
on a continuous stream?
可以grep
在连续流上使用吗?
What I mean is sort of a tail -f <file>
command, but with grep
on the output in order to keep only the lines that interest me.
我的意思是一种tail -f <file>
命令,但是grep
为了只保留我感兴趣的行在输出上。
I've tried tail -f <file> | grep pattern
but it seems that grep
can only be executed once tail
finishes, that is to say never.
我试过了,tail -f <file> | grep pattern
但似乎grep
只能在tail
完成后执行,也就是说永远不会。
采纳答案by tad
Turn on grep
's line buffering mode when using BSD grep (FreeBSD, Mac OS X etc.)
grep
使用 BSD grep(FreeBSD、Mac OS X 等)时打开的行缓冲模式
tail -f file | grep --line-buffered my_pattern
You don't need to do this for GNU grep (used on pretty much any Linux) as it will flush by default (YMMV for other Unix-likes such as SmartOS, AIX or QNX).
您不需要为 GNU grep(几乎在任何 Linux 上使用)执行此操作,因为它默认会刷新(YMMV 用于其他类 Unix,例如 SmartOS、AIX 或 QNX)。
回答by Irit Katriel
I use the tail -f <file> | grep <pattern>
all the time.
我tail -f <file> | grep <pattern>
一直都在用。
It will wait till grep flushes, not till it finishes (I'm using Ubuntu).
它会等到 grep 刷新,而不是直到它完成(我正在使用 Ubuntu)。
回答by Caleb
Yes, this will actually work just fine. Grep
and most Unix commands operate on streams one line at a time. Each line that comes out of tail will be analyzed and passed on if it matches.
是的,这实际上可以正常工作。Grep
并且大多数 Unix 命令一次一行地对流进行操作。如果匹配,将从尾部出来的每一行都将被分析并传递。
回答by XzKto
I think that your problem is that grep uses some output buffering. Try
我认为您的问题是 grep 使用了一些输出缓冲。尝试
tail -f file | stdbuf -o0 grep my_pattern
it will set output buffering mode of grep to unbuffered.
它将 grep 的输出缓冲模式设置为无缓冲。
回答by Atif
Use awk(another great bash utility) instead of grep where you dont have the line buffered option! It will continuously stream your data from tail.
在没有行缓冲选项的情况下,使用 awk(另一个很棒的 bash 实用程序)而不是 grep!它会不断地从尾部流式传输您的数据。
this is how you use grep
这就是你如何使用 grep
tail -f <file> | grep pattern
This is how you would use awk
这就是你将如何使用 awk
tail -f <file> | awk '/pattern/{print tail -f /var/log/some.log | grep --line-buffered foo | grep bar
}'
回答by Dale Anderson
In most cases, you can tail -f /var/log/some.log |grep foo
and it will work just fine.
在大多数情况下,您可以tail -f /var/log/some.log |grep foo
并且它会正常工作。
If you need to use multiple greps on a running log file and you find that you get no output, you may need to stick the --line-buffered
switch into your middlegrep(s), like so:
如果您需要在运行的日志文件上使用多个 grep 并且发现没有输出,您可能需要将--line-buffered
开关插入中间grep(s),如下所示:
tail -F <fileName> | grep --line-buffered <pattern> -A 3 -B 5
回答by mebada
you may consider this answer as enhancement .. usually I am using
您可以将此答案视为增强功能..通常我正在使用
tail -F <file> | less
-F is better in case of file rotate (-f will not work properly if file rotated)
-F 在文件旋转的情况下更好(如果文件旋转,-f 将无法正常工作)
-A and -B is useful to get lines just before and after the pattern occurrence .. these blocks will appeared between dashed line separators
-A 和 -B 可用于在模式出现之前和之后获取行.. 这些块将出现在虚线分隔符之间
But For me I prefer doing the following
但对我来说,我更喜欢做以下事情
tail -c +0 -f <file> | grep --line-buffered <pattern>
this is very useful if you want to search inside streamed logs. I mean go back and forward and look deeply
如果您想在流式日志中进行搜索,这将非常有用。我的意思是来回深入地看
回答by Christian Herr
sedwould be a better choice (streameditor)
sed将是更好的选择(流编辑器)
tail -n0 -f <file> | sed -n '/search string/p'
tail -n0 -f <file> | sed -n '/search string/p'
and then if you wanted the tail command to exit once you found a particular string:
然后,如果您希望在找到特定字符串后退出 tail 命令:
tail --pid=$(($BASHPID+1)) -n0 -f <file> | sed -n '/search string/{p; q}'
tail --pid=$(($BASHPID+1)) -n0 -f <file> | sed -n '/search string/{p; q}'
Obviously a bashism: $BASHPID will be the process id of the tail command. The sed command is next after tail in the pipe, so the sed process id will be $BASHPID+1.
显然是一种bashism:$BASHPID 将是tail 命令的进程ID。sed 命令在管道中的尾部之后,因此 sed 进程 ID 将为 $BASHPID+1。
回答by Ken Williams
If you want to find matches in the entirefile (not just the tail), and you want it to sit and wait for any new matches, this works nicely:
如果您想在整个文件中查找匹配项(不仅仅是尾部),并且您希望它坐下来等待任何新匹配项,这很有效:
less +F <file>
ctrl + c
/<search term>
<enter>
shift + f
The -c +0
flag says that the output should start 0
bytes (-c
) from the beginning (+
) of the file.
该-c +0
标志表示输出应从文件的开头 ( )开始0
字节 ( -c
) +
。
回答by Hans.Loven.work
Didn't see anyone offer my usual go-to for this:
没有看到有人为此提供我通常的选择:
##代码##I prefer this, because you can use ctrl + c
to stop and navigate through the file whenever, and then just hit shift + f
to return to the live, streaming search.
我更喜欢这个,因为您可以ctrl + c
随时停止和浏览文件,然后只需点击shift + f
即可返回实时流搜索。