Why can't I filter tail's output multiple times through pipes?

只愿长相守 提交于 2020-01-06 02:25:48

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/36822898/why-cant-i-filter-tails-output-multiple-times-through-pipes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!