As noted above by SuicideJunkie you don't start from the beginning of the second file for each pass trough the outer loop, i.e. for each line of file1.
in reply to nested loops to compare 2 files is only looping a limited number of times.
If your second file is not too big (compared to memory available) I would suggest to read the second file into a hash before the first loop and then loop through file1 and look for matches. You only need one loop then and not the two nested ones.