my %strings; while () { chomp; $string{$_} .= '1'; # if same data appears three times, hash value is "111"; } while () { chomp; $string{$_} .= '2'; # same as above, but with "2" instead of "1" } # get hash keys (lines) that occur in both files: my @common = grep { $strings{$_} =~ /12/ } sort keys %strings; # report findings: for my $key ( @common ) { my ( $n1, $n2 ) = ( $strings{$key} =~ /(1+)(2+)/ ); printf("%s found %d times in file1, %d times in file2\n", $key, length($n1), length($n2)); } # you can also pick out strings unique to file1 (/1$/) # and/or strings unique to file2 (/^2/), along with their # frequency of occurrence. This also scales fairly well to # handling three or more files.