You have a million records in the file with the 4 columns, or in the 40 files combined? How are your file1 .. file40 structured? Can the pairs of numbers be anywhere? Are the numbers unique? Appear together on a line?
in reply to Multiple Extraction from Multiple Files
You don't have to slurp in the first file all at once. You could do 40 passes, first doing all the entries of file1, then of file2, etc. Instead of 40 passes, you could also first sort the file. Or do a single pass, writing out the records to 40 different files, which you then process in order.
Whether you need to read in the other files, then depends on how they are structured.