help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] How to merge stdout and stderr yet distinguish what is f


From: Tadeus Prastowo
Subject: Re: [Help-bash] How to merge stdout and stderr yet distinguish what is from stdout and what is from stderr?
Date: Mon, 5 Feb 2018 10:57:56 +0100

If I can assume that a write is atomic at line level, then the
following should work:

exec {mystdout}>&1
coproc pA (sed 's%^%[From stdout] %' >&${mystdout})
coproc pB (sed 's%^%[From stderr] %' >&${mystdout})
coproc (echo 'Forget pB as pA has already been forgotten' >/dev/null)
cmd 1>&${pA[1]} 2>&${pB[1]}; exec {pA[1]}>&- {pB[1]}>&-

--
Best regards,
Tadeus


On Sun, Feb 4, 2018 at 10:34 PM, Peng Yu <address@hidden> wrote:
> Hi,
>
> `cmd 2>&1` can be used to merge stdout stderr into one stream. But the
> content from stdout and stderr are not distinguishable anymore.
>
> Is there a way to not only merge stdout and stderr but also prepend
> each line from stdout with a prefix and prepend each line from stderr
> with a different prefix (e.g., 'o' and 'e' respectively) so that they
> are distinguishable (we assume that there will be no merged lines
> coming from both stdout and stderr)?
>
> Maybe this can be done through an external program, but I am not sure
> how to implement, especially, if I want the order of the lines
> combined from both streams unchanged whether there are prefixes or
> not.
>
> Does anybody know a solution to this problem? Thanks.
>
> --
> Regards,
> Peng
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]