问题描述
意外的是,这种方式失败( 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缓冲它输出到管道到另一个命令,但不是如果它打印到标准输出。有关其他信息,请参阅。
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.
这篇关于为什么我不能通过管道多次过滤尾部的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!