Use a hash to compare the lines.
use strict;
use warnings;
my $f1 = 'E:\upload\new\2.txt';
my $f2 = 'E:\upload\new\a.txt';
my $outfile = 'E:\upload\new\1.txt';
my %results = ();
open FILE1, "$f1" or die "Could not open file: $! \n";
while(my $line = <FILE1>){
$results{$line}=1;
}
close(FILE1);
open FILE2, "$f2" or die "Could not open file: $! \n";
while(my $line =<FILE2>) {
$results{$line}++;
}
close(FILE2);
open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";
foreach my $line (keys %results) {
print OUTFILE $line if $results{$line} == 1;
}
close OUTFILE;
There are also some perl modules that will compare files, or you can use the operating system commands Salem mentions.
Hello Kevin,
I know this is really a old thread and i should really not be re-opening it. However, the solution you have provided is as mystical as a genie that i couldnt find another better code than the above. I have implemented the above code with certain changes however i require your help to tweak it a further more. Here is a short background,
1. I am trying to compare two text files and find the "missing records" and "mismatch" data in them. The above script gives both missing and mismatch but my requirement is to obtain them individually. What changes would i require to make it work so that it can return me missing data and mismatch data seperately.
Eg : Missing data : File A contains John, denver, marion
File B Contains John, denver
Eg : Mismatch Data: File A Contains John, denver, marion
File B Contains Johnm deeenver, mariion
Thanks much for your help.