Adding time-stamps to the outputs and sorting by them cannot work, for precisely the same reasons that cause all the other attempted methods to fail.
The data can sit in the output buffers of the original “yourcommand” for an arbitrarily long time, until the respective buffer is full and gets auto-flushed. So the process inserting the timestamps can only timestamp the rows with the time at which it gets the 4KB block. It cannot know what period has elapsed since the text was originally appended to each buffer.
And all those late times will most likely be identical as well as deferred (because you got a whole lot of lines at once). So sorting them with an identical time key (where the sort generally sorts on the rest of the line by default) will actually unsort all the lines in that block.
Also, the 4KB block will have part-lines that overlap another block, so when the files are merged the file will still contain many discontinuities.
You might revisit my post #7, dated Aug 31. It is impossible (both theoretically and practically) to solve this problem unless you can line-buffer both stdout and stderr as written by “yourcommand”.
As I have no idea what “yourcommand” actually does, we do not know how to make it line-buffer. You would need to run that under strace, and experiment with stdbuf, and post the results. That is what I did for post #7.
(a) If you have the source and it is low-level (C or C++) it is a one-line code fix. I don’t know how you would fix it in Python or Java, for example.
(b) If it uses stdio for every write on stdout and stderr, and does not mess with the buffering itself, then it should be effective to run it under control of stdbuf -o L. Every other process on the path to both.txt also must be line-buffered, for exactly the same reasons.
(c) You could have “yourcommand” insert timestamps on every line it wrote to either stdout or stderr. Actually, you cannot sort that output, because a sort requires all the data, in case the lowest key happens to be the last output.
In our situation, we know the times are monotonically ascending, so we can merge the two streams in time order. But that solution also has a sting in the tail.
Suppose the stderr only gets two lines written to it: Started at ymdhms, and Ended at ymdhms. That buffer perhaps only gets flushed after a six-hour run.
As we have not seen the Started record, we don’t know its timestamp. So we have to hold back the entire stdout data (all 55GB of it) until we see the Started record; put that out first; see the Ended record; put out the 55GB (because all those timestamps are earlier than Ended); put out the Ended record.
OK, that is an extreme case, but it illustrates the difficulty. If we could line-buffer the outputs, we would be able to merge the streams: but then if we could line-buffer the outputs, we would not need the timestamps either.
I don’t know how to explain this any more clearly, so here is an analogy.
There is a school trip, and you arrange for them to walk in single file, girls in one line, boys in the other, and they form an orderly queue. That should be tidy.
Next time, it is raining, so you send them down in several school busses. And each bus unloads all its pupils at the door, one bus at a time. So the busses form the queue, and the order that the children enter the event is nothing like the order in which they originally got on the busses.