I want to write stdout to a file but also prints stdout and stderr. I tried using tee:
prog | tee stdout.txt
but this causes the printed stderr and stdout to be interleaved incorrectly, i.e. if the correct output should be
OUT1 ERR1
OUT2 ERR2
OUT3 ERR3
OUT4 ERR4
using tee might print them out of order, e.g.:
ERR1 ERR2
OUT1
OUT2
OUT3 ERR3
OUT4
ERR4
Is there any way to print both stdout and stderr to screen and also write the stdout to a file without gobbling up the printed output?