如何通过 grep 将输入传送到另一个实用程序?
我正在使用“tail -f”来跟踪日志文件的更新; 接下来,我将其输出传递给 grep 以仅显示包含搜索词的行(在本例中为“org.springframework”); 最后我想做的是将 grep 的输出通过管道传输到第三个命令“cut”:
tail -f logfile | grep org.springframework | cut -c 25-
cut 命令将为我删除每行的前 25 个字符如果它可以从 grep 获取输入! (如果我从链中删除“grep”,它会按预期工作。)
我正在将 cygwin 与 bash 一起使用。
实际结果:当我添加第二个管道以连接到“cut”命令时,结果是它挂起,就好像它正在等待输入(如果您想知道)。
I am using 'tail -f' to follow a log file as it's updated; next I pipe the output of that to grep to show only the lines containing a search term ("org.springframework" in this case); finally I'd like to make is piping the output from grep to a third command, 'cut':
tail -f logfile | grep org.springframework | cut -c 25-
The cut command would remove the first 25 characters of each line for me if it could get the input from grep! (It works as expected if I eliminate 'grep' from the chain.)
I'm using cygwin with bash.
Actual results: When I add the second pipe to connect to the 'cut' command, the result is that it hangs, as if it's waiting for input (in case you were wondering).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设使用 GNU grep,将
--line-buffered
添加到命令行,例如。编辑:
我发现 grep 缓冲并不是这里唯一的问题,因为 cut 不允许逐行缓冲。
您可能想尝试将其替换为您可以控制的内容,例如 sed:
或 awk
Assuming GNU grep, add
--line-buffered
to your command line, eg.Edit:
I see grep buffering isn't the only problem here, as cut doesn't allow linewise buffering.
you might want to try replacing it with something you can control, such as sed:
or awk
在我的系统上,在获得任何输出之前,大约 8K 已被缓冲。 此序列可以立即跟踪该文件:
On my system, about 8K was buffered before I got any output. This sequence worked to follow the file immediately:
你所拥有的应该可以正常工作——这就是管道的全部理念。 我看到的唯一问题是,在我拥有的
cut
版本(GNU coreutiles 6.10)中,您应该使用语法cut -c 25-
(即使用减号号而不是加号)以删除前 24 个字符。您还在两个示例中搜索不同的模式,以防相关。
What you have should work fine -- that's the whole idea of pipelines. The only problem I see is that, in the version of
cut
I have (GNU coreutiles 6.10), you should use the syntaxcut -c 25-
(i.e. use a minus sign instead of a plus sign) to remove the first 24 characters.You're also searching for different patterns in your two examples, in case that's relevant.