How to prevent pipe leakage?

When I run the command below in bash, it waits until the program finishes before releasing all the output. If I remove the pipes, it will immediately print each line.

{ for i in `seq 3` ; do echo $i ; sleep 1 ; done ; } \
    | perl -p -e 's,(.*ERROR.*),\e[01;31m\1\e[00m,g' \
    | perl -p -e 's,(.*WARNING.*),\e[01;33m\1\e[00m,g' \
    | perl -p -e 's,(.*TCPEchoTest.*),\e[01;30m\1\e[00m,g' \
    | perl -p -e 's,(.*enters.*),\e[00;33m\1\e[00m,g'

How can I use pipes and does everyone print each line at once?

+3
source share
4 answers

You can combine several lines of perl code with repeated parameters -e (be sure to complete them with ;- they are strung together to form a program). And you can make the pipes "hot" with $|=1. See the perl$| manual for details (2/3 down the page, search for OUTPUT_AUTOFLUSH).

{ for i in `seq 3` ; do echo $i ; sleep 1 ; done ; } \
  | perl -p -e 'BEGIN{$|=1};' \
            -e 's,(.*ERROR.*),\e[01;31m\1\e[00m,g;' \
            -e 's,(.*WARNING.*),\e[01;33m\1\e[00m,g;' \
            -e 's,(.*TCPEchoTest.*),\e[01;30m\1\e[00m,g;' \
            -e 's,(.*enters.*),\e[00;33m\1\e[00m,g;'

1, 2, 3 . , BEGIN , perl . , .

+2

4 , , .

stdbuf, , :

stdbuf -i0 -o0 -e0 command | perl ...

. :

+4

perl , perl - , , . $| perl, :

{ for i in `seq 3` ; do echo $i ; sleep 1 ; done ; } \
    | perl -p -e 'BEGIN{$|=1}s,(.*ERROR.*),\e[01;31m\1\e[00m,g' \
    | perl -p -e 'BEGIN{$|=1}s,(.*WARNING.*),\e[01;33m\1\e[00m,g' \
    | perl -p -e 'BEGIN{$|=1}s,(.*TCPEchoTest.*),\e[01;30m\1\e[00m,g' \
    | perl -p -e 'BEGIN{$|=1}s,(.*enters.*),\e[00;33m\1\e[00m,g'

BEGIN , , , , , -p.

+4

, , colortail, colorize ccze.

, libc , stdout . . .

, , , :

Wumpus, :

  • $|=1 STDOUT->autoflush(1) Perl
  • fflush() awk
  • sys.stdout.flush() Python

:

  • -u Python GNU sed
  • --line-buffered GNU awk

Opening PTY for the terminal, creating stdout TTY and circumventing the problem:

  • script -q /dev/null yourcommand
  • unbuffer of expect package

Finally, it $LD_PRELOADbreaks the injection options into the C runtime (neatly wrapped in GNU coreutils stdbuf, suggested by Vilhelm)

+3
source