How to use sed to manipulate continuously streaming output?

Chances are that the command's output is buffered. When the command writes to a terminal, the buffer is flushed on every newline, so you see it appear at the expected rate. When the command writes to a pipe, the buffer is only flushed when it reaches a few kilobytes, so it lags a lot. Thus is the default behavior of the standard input/output library.

To force the command not to buffet its output, you can use unbuffer (from expect) or stdbuf (from GNU coreutils).

unbuffer command | sed …
stdbuf -o0 command | sed …

sed has an option for that :

-u, --unbuffered

Which loads minimal amounts of data from the input files and flushes the output buffers more often. See man sed for more details.


I would use awk

  command | awk '/some important stuff/ { printf "%c[31m%s%c[0m\n",27,$0,27 ; next }
  { print ; } '

where

  • /some important stuff/ select important line, like in sed
  • printf "%c[31m%s%c[0m\n",27,$0,27 ; print in red
    • use 32,33 for green, yellow ...
    • $1, $2, can be use to select a specific field
  • other line are just printed 'as is'

the key point is that command should flush lines, but that should be the case if you have lot of output.