merge sorted files, with minimal buffering

Viewed 93

I have two log files that are prefixed with a sortable timesetamp. I'd like to see them, in order, while the processes generating the log files are still running. This is a pretty faithful simulation of the situation:

slow() {
    # print stdout at 30bps
    exec pv -qL 30
}
timestamp() {
    # prefix stdin with a sortable timestamp
    exec tai64n
}

# Simulate two slowly-running batch jobs:
seq 000 099 | slow | timestamp > seq.1 &
seq1=$!
seq 100 199 | slow | timestamp > seq.2 &
seq2=$!

# I'd like to see the combined output of those two logs, in timestamp-sorted order
try1() {
    # this shows me the output as soon as it's available,
    # but it's badly interleaved and not necessarily in order
    tail -f seq.1 --pid=$seq1 &
    tail -f seq.2 --pid=$seq2 &
}
try2() {
    # this gives the correct output,
    # but outputs nothing till both jobs have stopped
    sort -sm <(tail -f seq.1 --pid=$seq1) <(tail -f seq.2 --pid=$seq2)
}


try2
wait
1 Answers
Related