[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: |
Peng Yu |
Subject: |
Re: [Help-bash] How to merge stdout and stderr yet distinguish what is from stdout and what is from stderr? |
Date: |
Sun, 4 Feb 2018 18:03:37 -0600 |
On Sun, Feb 4, 2018 at 4:33 PM, Andy Chu <address@hidden> wrote:
>
>
> On Sun, Feb 4, 2018 at 1: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)?
>
>
>
> Joao just gave the same answer as I was typing it, but:
>
> https://github.com/oilshell/blog-code/blob/master/stdout-stderr/demo.sh
>
> I don't like process substitution that much, because the syntax looks odd.
> For me it helps to think of it as
>
> prog > $stdin(awk ... ) 2> $stdin(awk ...) # imaginary syntax
>
> The $ makes it clearer that it expands to a word like /dev/fd/64. >() is
> confusing especially when preceded by the redirect operators!
>
>
> $ ./demo.sh prog
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
>
> $ ./demo.sh filter
> o 1
> o 2
> o 3
> e 4
> e 5
> e 6
> o 7
> o 8
> o 9
I tried it multiple times. But the order is not guaranteed to be
maintained, I guess it is due the buffer that might be used with awk
when reading from stdout or stderr. So technically speaking, this
solution does not solve my original question.
--
Regards,
Peng