Search code examples
linuxbashstdoutstderrio-redirection

Redirection std_out, std_err with(out) grep


I have some trouble with the redirection stuff in Linux, here a small schematic to illustrate the goal, better than words (I hope)

+------------+                              +----------------+
|   std_out  +----------+------------------>+     log.log    |
+------------+          |                   +----------------+
                        |
                        |                   +----------------+
                        +----------------->>+                |
                                            |   histo.log    |
                        +----------------->>+                |
                        |                   +----------------+
                        |
                        |
 +------------+         |   grep -v "exp"   +----------------+
 |   std_err  +---------+------------------>+   error.log    |
 +------------+                             +----------------+

I would like to do this with the output of the script. I succeed to do the log.log and the error.log with the grep,

./script.sh >log.log 2> >(grep -v "Expression" > error.log )

But I'm in trouble with histo.log where it should add (i mean >> or tee -a not >) std_out and the std_err (without the grep) and I don't really understand the advanced stuff about redirection.

If anyone have an idea to solve my problem, I will be thankfull :)


Solution

  • Here's a scary looking use of output process substititions:

    $ cat driver.sh
    echo line 1 to stdout
    echo line 1 to stderr >&2
    echo line 2 to stderr is expected >&2
    echo line 3 to stderr >&2
    
    $ sh driver.sh > >(tee -a histo.log > log.log) 2> >(tee -a histo.log | grep -v exp > error.log)
    
    $ cat log.log
    line 1 to stdout
    
    $ cat histo.log
    line 1 to stdout
    line 1 to stderr
    line 2 to stderr is expected
    line 3 to stderr
    
    $ cat error.log
    line 1 to stderr
    line 3 to stderr
    

    This will append to histo.log, but overwrite the log.log and error.log files, as you indicated in your diagram.