Pipe only STDERR through a filter

后端 未结 7 1051
夕颜
夕颜 2020-11-28 20:53

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

STDOUT ────────────────┐
                       ├         


        
7条回答
  •  清酒与你
    2020-11-28 21:19

    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
    

提交回复
热议问题