I would like to compare two files, say file1 and file2, and output two new files, say file1.out and file2.out, with the lines that are in common (according to diff file1 file2) occurring first, and then the lines in file1 that are not in file2 appended at the end of file1.out, and the lines in file2 but not in file1 appended at the end of file2.out.
For example, let's say I have file1:
A
B
C
E
and file2:
A
C
D
E
I would like to common lines, A, C, and E to come first in modified files file1.out and file2.out, in their original order, and the distinct lines, B and D respectively, to be moved to the end. With my example, that would yield file1.out:
A
C
E
B
and file2.out:
A
C
E
D
More generally, my input files might have thousands of lines that are mostly the same, with some scattered differences that I would like pushed at the end for easier visual inspection.
I have looked at related-type queries such as here (Compare two files line by line and generate the difference in another file) but I did not find the solution I am looking for here. If you know how to generate output as described above, that would be greatly appreciated.