1

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?

user569825
  • 2,369
  • 1
  • 25
  • 45
  • Possible duplicate of [Piping tail output though grep twice](https://stackoverflow.com/questions/13858912/piping-tail-output-though-grep-twice) – tripleee Feb 01 '19 at 13:08

1 Answers1

2

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.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • Thanks, Ed. The article suggests to first check if there are unnecessary commands that may be eliminated. In my particular case, I was able to do that adding `--silent` and the explicitly printing substituted lines (`p`). Eg `sed --silent 's#pl#st#p'`. (Otherwise, the mentioned *unbuffered* variants would've been the way to go.) – user569825 Apr 25 '16 at 22:40
  • I've never heard of `--silent`, looks like it does the same thing as the more common `-n`. That will print lines that contain `pl` in context other than `played` thought. You can do things like `/played/{s/pl/st/gp}` with sed though check the syntax. In awk it'd be `awk '/played/{gsub(/pl/,"st");print}'`. – Ed Morton Apr 25 '16 at 23:13
  • Luckily my use case is more complex than the example I gave. Thus every line that receives substitutions is actually desired in the output. Still, for future cases I'd be curious about the correct syntax for sed. (Btw `--silent` *is* `-n`. I just prefer using the elaborate variants of switches when scripting and documenting.) – user569825 Apr 25 '16 at 23:33