I have two files that are several hundred MB in size. These files each contain a sorted key. What I would like to do is read each line from each file one at a time. If a match is found, write it out to a “matched” file otherwise write to one of two “nonmatch” files. The main issue that I need to avoid is reading the files into memory. I think I understand how I want to test the condition, what I don't understand is how the looping will work.
input files
file0 file1
1 2
2 4
3 5
5 8
7 9
9
logical test
1<=>2 -1 next 0, keep 1, print to nomatch0
2<=>2 0 next 0, next 1, print to match
3<=>4 -1 next 0, keep 1, print to nomatch0
5<=>4 1 keep 0, next 1, print to nomatch1
5<=>5 0 next 0, next 1, print to match
7<=>8 -1 next 0, keep 1, print to nomatch0
9<=>8 1 keep 0, next 1, print to nomatch1
9<=>9 0 next 0, next 1, print to match
output files
matched nomatch0 nomatch1
2 1 4
5 3 8
9 7