Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe only STDERR through a filter

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 ────┘ 
like image 310
Martin DeMello Avatar asked Sep 01 '10 12:09

Martin DeMello


People also ask

How do you separate stdout and stderr?

As redirection is a method of capturing a program output and sending it as an input to another command or file. The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator.

Can stderr be redirected?

The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.

How do you calculate stderr?

The standard error is calculated by dividing the standard deviation by the sample size's square root. It gives the precision of a sample mean by including the sample-to-sample variability of the sample means.


1 Answers

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)
like image 152
Paul Rubel Avatar answered Oct 14 '22 11:10

Paul Rubel