[mdlug] [HOWTO request] filter stderrr without altering stdout

Flavio daCosta flav at binaryservice.com
Wed Apr 11 12:58:12 EDT 2007


>From an email I sent to the list a few years back.  Its bash and not
sure if applicable in your case, but here it is:


On Tue, 2004-01-13 at 07:03, Flavio daCosta wrote:
[]$ (ls -ld /etc /no_such_dir 2>&3 | sed 's/^/out pipe:/') \
        3>&1 1>&2 | sed 's/^/err pipe:/'

A little more explanation:

File descriptors 0,1,2 (STDIN, STDOUT,STDERR) are common, but there are
more which may be used (3-9).   These may be used as a kind of holding
place.  The command above in psudo-code would read as such:

(the parenthesis make the processes run the following two commands in a
sub shell)
run command and re-direct STDERR (2>&3) to holding place '3' and
pipe STDOUT.  I piped it to a sed script which substitutes 'out pipe:'
at the beginning of the line.

Then take what went to 3 and send that to STDOUT (3>&1) and for safety
take the output of the earlier commands and send that to STDERR (1>&2).
At this point the former STDERR is simply STDOUT and we can just pipe
this as usual.

flav



More information about the mdlug mailing list