Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
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
add comment

1 Answer 1

up vote 3 down vote accepted

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.

unbuffer

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

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

stdbuf

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

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"
share|improve this answer
    
Awesome, thanks. –  Mikulas Dite Aug 13 '13 at 8:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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