Search code examples
sedgreppipetail

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


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?


Solution

  • 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.