Does a simple one liner exist for grepping continuous output from a program rather than just a file?

Now just as an example, I would like to skip lines containing PHPUnit in them. For obvious reasons the following gist does not do that:

phpunit tests/cases | grep -v --line-buffered "PHPUnit"

Can it be done as a universally usable oneliner, without user defined functions and so?

  • 1
    Why does it not work? – Stéphane Chazelas Aug 12 '13 at 21:14
  • @StephaneChazelas oh the test prints the content over like 15 minutes, by characters. I have no idea how grep is implemented, but I guess it can't really do much once the characters are printed. – Mikulas Dite Aug 12 '13 at 21:29
  • 1
    May be the test cases output to STDERR? How does phpunit test/cases 2>&1 | grep -v "PHPUnit" fair? – Drav Sloan Aug 12 '13 at 21:32
  • 1
    So what's going on: do you get no output at all? too much output? the right output but delayed? something else? – Gilles Aug 12 '13 at 23:12

This sounds like the output from your phpunit command is getting buffered (either by PHP, the Bash shell, or grep). There are a couple of ways to unbuffer the output so that it's more responsive.


You could try using the unbuffer command which is part of expect.

$ unbuffer phpunit tests/cases | grep -v --line-buffered "PHPUnit"


You can try using the stdbuf command to disable the buffering.

$ stdbuf -i0 -o0 -e0 phpunit tests/cases | grep -v --line-buffered "PHPUnit"

Details of the switches:

   -i, --input=MODE
          adjust standard input stream buffering

   -o, --output=MODE
          adjust standard output stream buffering

   -e, --error=MODE
          adjust standard error stream buffering

So the above sets the buffering for STDIN, STDOUT, STDERR is set to zero.


Grep itself might be the culprit in buffering output. You can tell it to buffer only a line at a time's worth of output. (This one you've already mentioned/discovered but I'm mentioning it so that future viewers of this Q&A will understand that there is buffering there too).

$ phpunit tests/cases | grep -v --line-buffered "PHPUnit"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.