Code:
#!/usr/bin/perl
use warnings;
my $input_file1 = shift;
my $input_file2 = shift;
my $primary_col;
my $starttime = shift;
#my $delim = shift;
open FIRFILE, $input_file1 or die "Unable to open file: [$!]";
open SECFILE, $input_file2 or die "Unable to open file: [$!]";
open(OUTFILE,"+>compareoutput_$starttime.txt") or die "Can't Create File!!";
$primary_col = `head -1 pk_count.txt`; # To get the number of primary keys
# Start with empty hashes
my %firHash = ();
my %secHash = ();
#print "First File:\n";
# Fill the first hash
while (<FIRFILE>)
{
@fileColumns1 = split(/,/);
#@fileColumns1 = split(/$delim/);
my $size1 = @fileColumns1;
#my $ident = $fileColumns1[0];
my $ident2 = join(",", @fileColumns1[0..$primary_col-1]);
#my $ident2 = join("$delim", @fileColumns1[0..$primary_col-1]);
$firHash{$ident2} = $_;
}
# Fill the second hash
while (<SECFILE>)
{
@fileColumns2 = split(/,/);
#@fileColumns2 = split(/$delim/);
my $size2 = @fileColumns2;
#my $ident = $fileColumns2[0];
my $ident2 = join(",", @fileColumns2[0..$primary_col-1]);
#my $ident2 = join("$delim", @fileColumns2[0..$primary_col-1]);
$secHash{$ident2} = $_;
}
foreach $key1 (sort keys %firHash)
{
foreach $key2 (sort keys %secHash)
{
if ($key1 eq $key2)
{
my @file2 = split(/,/, $secHash{$key2});
#my @file2 = split(/$delim/, $secHash{$key2});
delete $file2[0];
my @file1 = split(/,/, $firHash{$key1});
#my @file1 = split(/$delim/, $firHash{$key1});
delete $file1[0];
my $len = @file1;
for ($count=$primary_col; $count<$len; $count++)
{
chomp $file1[$count];
chomp $file2[$count];
my $column_num = $count+$primary_col-1;
if ( $file1[$count] ne $file2[$count])
{
print OUTFILE $key1,",",$column_num,",",$file1[$count],",",$file2[$count],",N","\n";
#print OUTFILE $key1,$delim,$column_num,$delim,$file1[$count],$delim,$file2[$count],$delim,"N","\n";
}
else
{
print OUTFILE $key1,",",$column_num,",",$file1[$count],",",$file2[$count],",Y","\n";
#print OUTFILE $key1,$delim,$column_num,$delim,$file1[$count],$delim,$file2[$count],$delim,"Y","\n";
}
}
}
}
}