Re: Filter stdout and stderr Together



On Thu, 14 Aug 2008 10:07:57 -0700, Scott Wegner wrote:

Hi All,

I would like to write a script that filters lines from both stdout and
stderr, and output the lines in the same order as they were originally
submitted. I've tried a few times using some complex stream redirection
and "grep -v ...", although I can't seem to get the results I want *in
order*. Has anybody done this before? Essentially I want a shell
script with the following syntax:

myscript command params

where myscript is the script I'm writing. command and params is what
should be executed. Output from the command should come on stdout and
stderr as expected, but with particular lines filtered out. My first
attempt was something along the lines of:

#!/usr/bin/env bash
MYFILTER="uninteresting output"

((($@) | grep -v "$MYFILTER") 3>&1 1>&2 2>&3 | grep -v "$MYFILTER") 2>&1
1>&3 3>&2

However, this was outputting text in the wrong order. Anybody know what
I'm doing wrong?

Scott

If you don't mind losing the distinction between stdout and stderr,
easiest is probably to:

eval "$@" 2>&1 | grep -v "$MYFILTER"

If you do mind losing the distinction, it gets harder. In that case, you
might end up writing in more of a general purpose language like python
(using the powerful subprocess module) or similar, and reading from the
two file descriptors in a select loop or with two threads.

.



Relevant Pages