As a slight note,
Moritz’s solution as-written does not seem to consider what to do with the leftover records in the longer of the two files. The necessary additions, to be placed after what is shown, are trivial ... the only trick being to ensure that the first leftover record is processed.

This thing is easy if you know that each file will have an exact match of records. Much less easy if you can have missing lines in one of the files or the other.

I wrote a program doing comparison between to very large files, handling all the cases of records existing in one file and not in the other with all the special cases (file A finished before B, or the other way) is not really trivial.

I am working on transforming this program into a module as generic as possible, but, unfortunately, it is not ready to be used.