Is there any way, in bash, to pipe stderr through a filter before unifying it with stdout? That is, I want

stdout ----------------\
                        |-----> terminal/file/whatever
stderr -- [ filter ] --/

rather than

stdout ----\
            |----[ filter ]---> terminal/file/whatever
stderr ----/
share|improve this question
1  
See also How to pipe stderr, and not stdout?. – Andrew Marshall May 22 '13 at 1:44
up vote 43 down vote accepted

Here's an example, modeled after how to swap file descriptors in bash . The output of a.out is the following, without the 'STDXXX: ' prefix.

STDERR: stderr output
STDOUT: more regular

./a.out 3>&1 1>&2 2>&3 3>&- | sed 's/e/E/g'
more regular
stdErr output

Quoting from the above link:

  1. First save stdout as &3 (&1 is duped into 3)
  2. Next send stdout to stderr (&2 is duped into 1)
  3. Send stderr to &3 (stdout) (&3 is duped into 2)
  4. close &3 (&- is duped into 3)
share|improve this answer
1  
Additional reference: BashFAQ/047 – Dennis Williamson Sep 1 '10 at 15:50
4  
This works with any POSIX shell, not just Bash. – Rufflewind Feb 1 '15 at 2:51

The last part of this page of the Advanced Bash Scripting Guide is "redirecting only stderr to a pipe".

# Redirecting only stderr to a pipe.

exec 3>&1                              # Save current "value" of stdout.
ls -l 2>&1 >&3 3>&- | grep bad 3>&-    # Close fd 3 for 'grep' (but not 'ls').
#              ^^^^   ^^^^
exec 3>&-                              # Now close it for the remainder of the script.

# Thanks, S.C.

This may be what you want. If not, some other part of the ABSG should be able to help you, it is excellent.

share|improve this answer
    
We are somewhat hesitant to recommend the ABSG as a reference, as it mixes documentation, prescription, and opinion without clearly marking the differences. Some sections also have dubious content, although the one you link to seems fine. – tripleee Mar 7 '17 at 7:59

A naive use of process substitution seems to allow filtering of stderr separately from stdout:

:; ( echo out ; echo err >&2 ) 2> >( sed s/^/e:/ >&2 )
out
e:err

Note that stderr comes out on stderr and stdout on stdout, which we can see by wrapping the whole thing in another subshell and redirecting to files o and e

( ( echo out ; echo err >&2 ) 2> >( sed s/^/e:/ >&2 ) ) 1>o 2>e
share|improve this answer
    
why the :; at the beginning? I tried the magic line without, and doesn't seem to make a difference. – Koshmaar Feb 8 '16 at 17:29
    
Some people use $ as a command prompt. Then they often write shell examples like: $ cat /var/log/syslog | fgrep .... However, this line is not copy-pastable because of the $. :; looks like a prompt but is basically a shell no-op; so you can select and paste the whole line safely. – solidsnack Feb 12 '16 at 9:44
9  
Ok, but you could just omit the :; and the line would be also copy-pastable :) To me :; doesn't look like a prompt, it was not making the example clearer (separating commands from output) but confusing. Though I understand your point of view and let's not discuss syntax/conventions. – Koshmaar Feb 19 '16 at 16:31

I find the use of bash process substitution easier to remember and use as it reflects the original intention almost verbatim. For example:

$ cat ./p
echo stdout
echo stderr >&2
$ ./p 2> >(sed -e 's/s/S/') | sed 's/t/T/'
sTdout
STderr

uses the first sed command as a filter on stderr only and the second sed command to modify the joined output.

Note that the white space after 2> is mandatory for the command to be parsed correctly.

share|improve this answer

Take a look at named pipes:

$ mkfifo err
$ cmd1 2>err |cat - err |cmd2
share|improve this answer
    
won't cat - err break the interspersing of stdout and stderr? – Martin DeMello Sep 1 '10 at 14:15
    
@Martin - it depends. If cmd1, cat, or cmd2 buffers output, then you could see output out of sequence. – mob Sep 1 '10 at 22:31

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.