File Comparison

Hi i have 2 csv files a.csv and b.csv with the same number of columns and a list of values in both of it. Each and every individual value in both the files need to compared and if it matches then print correct in a new csv file otherwise print Incorrect

eg

a.csv
1,12/27/2007,Reward,$10.00
2,12/27/2007,Reward,$20.00
3,12/27/2007,Reward,$30.00
4,12/27/2007,Commission,$40.00
5,12/28/2007,Reward,$10.00
6,12/27/2007,Reward,$20.00
7,12/27/2007,Reward $30.00

b.csv
1,12/27/2007,Reward,$10.00
2,12/27/2007,Reward,$20.00
3,12/27/2007,Reward,$30.00
4,12/27/2007,Reward,$40.00
5,12/28/2007,Reward,$10.00
6,12/27/2007,Reward,$20.00
7,12/27/2007,Reward $30.00

After comparing these two csv files the final comparison csv file should look like this

Correct,Correct,Correct,Correct
Correct,Correct,Correct,Correct
Correct,Correct,Correct,Correct
Correct,Correct,Incorrect,Correct
[SIZE=1]Correct,Correct,Correct,Correct
[SIZE=1]Correct,Correct,Correct,Correct
[SIZE=1]Correct,Correct,Correct,Correct

Im new to Shell script and awk programming. Suggest me acode with explanation for this requirement in either awk programming or shell scripting.

Please do make it soon its very urgent

Looking forward for your quick response

Regards,
Naveen
[/SIZE][/SIZE][/SIZE]

Hi Naveen,

Try this..
awk -F',' 'FNR==NR {_[$1]=$0;next} 
                   { split(_[$1],ar);
                     for(i=1;i<=NF;i++) 
                        if(ar!=$i) $i="Incorrect"; 
                        else  $i="Correct"; 
                     print 
                   }' a.csv b.csv

Regards,

Ranjith

Ranjith thank you very much for your response

Can you please explain me the coding logic also can you tell me how is the comparison values are written to the final csv file

@ Ranjith
awk with OFS

awk 'BEGIN{FS=OFS=","}FNR==NR {_[$1]=$0;next}{split(_[$1],ar);for(i=0;++i<=NF;) $i=(ar!=$i)?"Incorrect":"Correct"}1' a.csv b.csv

Thanks Danmero

The Code is working fine but im not able to export the Correct and Incorrect values to a csv file, can you please help me on that.

Looking forward for your response.

Regards,
Naveen

---------- Post updated at 11:37 AM ---------- Previous update was at 11:32 AM ----------

I checked the code. If any cell varies then the entire column is printed as Incorrect. But i need tthe individual cell comparison. please hell me out

Regards,
Naveen

FileArray.pm:

package FileArray;
sub TIEARRAY{
	my ($class,$file) =(@_);
	my @arr;
	_open($file,\@arr);
	return bless \@arr, $class;
}
sub FETCH{
	my ($self,$id1)=(@_);
	return $self->[$id1];
}
sub FETCHSIZE{
	my $self=shift;
	return $#{$self};
}
sub _open{
	my ($file,$ref)=(@_);
	open $fh,"<",$file;
	while(<$fh>){
		chomp;
		my @tmp = split(",",$_);
		$ref->[$.]=\@tmp;
	}
	close $fh;
}
1

compare.pl:

use FileArray;
my (@arr,@brr);
tie @brr,"FileArray","f2.txt";
tie @arr,"FileArray","f1.txt";

for(my $i=1;$i<=$#arr+1;$i++){
	for(my $j=0;$j<=$#{$brr[$i]};$j++){
		#print $brr[$i]->[$j],"<->",$arr[$i]->[$j];
		if ( $brr[$i]->[$j] eq $arr[$i]->[$j]){
			print "correct,";
		}
		else{
			print "WRONG,";
		}
	}
	print "\n";
}