my $file1 = "file1.txt"; my $file2 = "file2.txt"; my $file3 = "output.txt"; my $match = 0; # Open the files for reading open( FILE1, $file1 ); open( FILE2, $file2 ); while ( ) { my $string1 = $_; my $string2 = ; print "$counter: $string1\n$string2\n\n\n"; ++$match if ( $string1 eq $string2 ); } close FILE1; close FILE2; if ( $match ) { open( FILE3, '> $file3' ); print FILE3 "$match lines matched\n"; close FILE3; }