为什么我不能通过管道多次过滤尾部的输出? [英] Why can't I filter tail's output multiple times through pipes?

查看:144
本文介绍了为什么我不能通过管道多次过滤尾部的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

意外的是,这种方式失败( no output ;在 sh zsh bash p>

echofoo\\\
played\\\
bar> / tmp / t&& tail -f / tmp / t | grep播放| sed'#p#st#g'



注意,两次 grep 也会失败,表示它是完全不相关的命令被使用:

 #echo -efoo\\\
played\\\
bar> / tmp / t&& tail -f / tmp / t | grep播放| grep发挥作用:

grep

 #echo -efoo\\\
played\\\
bar> / tmp / t&& tail -f / tmp / t | grep演奏
演奏

sed 单独运作:

 #echo -efoo\\\
played\\\
bar> / tmp / t&& tail -f / tmp / t | sed's#pl#st#g'`
foo
stay
bar

使用 cat 而不是 tail ,它可以工作:

 #echo -efoo\\\
played\\\
bar> / tmp / t&& cat / tmp / t | grep播放| sed's#pl#st#g'
stay

使用 journalctl --follow ,它就像 tail 一样失败。

无法连接两次的原因是什么

解决方案

这是一个缓冲问题 - 第一个grep缓冲它输出到管道到另一个命令,但不是如果它打印到标准输出。有关其他信息,请参阅 http://mywiki.wooledge.org/BashFAQ/009


Unexpectedly, this fails (no output; tried in sh, zsh, bash):

echo "foo\nplayed\nbar" > /tmp/t && tail -f /tmp/t | grep played | sed 's#pl#st#g'

Note that two times grep also fails, indicating that it's quite irrelevant which commands are used:

# echo -e "foo\nplayed\nbar" > /tmp/t && tail -f /tmp/t | grep played | grep played

grep alone works:

# echo -e "foo\nplayed\nbar" > /tmp/t && tail -f /tmp/t | grep played
played

sed alone works:

# echo -e "foo\nplayed\nbar" > /tmp/t && tail -f /tmp/t | sed 's#pl#st#g'`
foo
stayed
bar

With cat instead of tail, it works:

# echo -e "foo\nplayed\nbar" > /tmp/t && cat /tmp/t | grep played | sed 's#pl#st#g'
stayed

With journalctl --follow, it fails just like with tail.

What's the reason for being unable to pipe twice?

解决方案

It's a buffering issue - the first grep buffers it's output when it's piping to another command but not if it's printing to stdout. See http://mywiki.wooledge.org/BashFAQ/009 for additional info.

这篇关于为什么我不能通过管道多次过滤尾部的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆