如何通过 grep 将输入传送到另一个实用程序?

发布于 2024-07-23 01:25:17 字数 416 浏览 9 评论 0原文

我正在使用“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 技术交流群。

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

发布评论

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

评论(3

旧人哭 2024-07-30 01:25:17

假设使用 GNU grep,将 --line-buffered 添加到命令行,例如。

tail -f logfile | grep --line-buffered org.springframework | cut -c 25-

编辑:

我发现 grep 缓冲并不是这里唯一的问题,因为 cut 不允许逐行缓冲。

您可能想尝试将其替换为您可以控制的内容,例如 sed:

tail -f logfile | sed -u -n -e '/org\.springframework/ s/\(.\{0,25\}\).*$/\1/p'

或 awk

tail -f logfile | awk '/org\.springframework/ {print substr($0, 0, 25);fflush("")}'

Assuming GNU grep, add --line-buffered to your command line, eg.

tail -f logfile | grep --line-buffered org.springframework | cut -c 25-

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:

tail -f logfile | sed -u -n -e '/org\.springframework/ s/\(.\{0,25\}\).*$/\1/p'

or awk

tail -f logfile | awk '/org\.springframework/ {print substr($0, 0, 25);fflush("")}'
农村范ル 2024-07-30 01:25:17

在我的系统上,在获得任何输出之前,大约 8K 已被缓冲。 此序列可以立即跟踪该文件:

tail -f logfile | while read line ; do echo "$line"| grep 'org.springframework'|cut -c 25- ; done

On my system, about 8K was buffered before I got any output. This sequence worked to follow the file immediately:

tail -f logfile | while read line ; do echo "$line"| grep 'org.springframework'|cut -c 25- ; done
橘亓 2024-07-30 01:25:17

你所拥有的应该可以正常工作——这就是管道的全部理念。 我看到的唯一问题是,在我拥有的 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 syntax cut -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.

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