Pipe only STDERR through a filter

后端 未结 7 1062
夕颜
夕颜 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:13

    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.

提交回复
热议问题