Finding difference in two comma separated files in UINX

Dear All,

I have requirement like this:
I have 2 camma seperated files
file1:

1,aaa,bbb,ccc,
2,bbb,ccc,ddd,
3,ccc,ddd,eee,

file2:

1,aaa,bbb,ccc,
2,bbb,ddd,ddd,
3,ccc,ddd,eee,

my requirement is I should get message in the out put like:

There is a difference in 3 rd filed in second record expected is ccc but actual is ddd .

How we can do it in unix.
I have written below code:

 export file1=$1
 export file2=$2
 export i=1
 rec_count1=`wc -l <  $file1`
 rec_count2=`wc -l <  $file2`
 
 while [  $i -le $rec_count1 ]
 do
  head -$i $file1 |tail -1 >temp_1.txt
  head -$i $file2 |tail -1 >temp_2.txt
  export j=1
  column_no1=`awk -F"," '{print NF}' temp_1.txt`
  column_no2=`awk -F"," '{print NF}' temp_2.txt`
  if [ $column_no1 != $column_no2 ]; then 
   echo "There is difference in no of fileds in record $i"
  else
  while [  $j -le $column_no1 ]
  do
   field1=`cut -f$j -d"," temp_1.txt`
   field2=`cut -f$j -d"," temp_2.txt`
   
   if [ "$field1" != "$field2" ];  then
    echo " There is a difference in column no=$j in record  no=$i  expected value is $field1 but actual value is $field2"
    
    
   fi
  j=`expr $j + 1`
  
  done
  fi
 i=`expr $i + 1`
 done

but it is taking lot of time for big files, How can we achive in better way, Please advise.

Thanks in Advance,
Moto :slight_smile:

why dont you use diff command ?

 
bash-3.00$ diff 1.txt 2.txt
2c2
< 2,bbb,ccc,ddd,
---
> 2,bbb,ddd,ddd,

if I use diff I will be getting whole record and again it is difficult for me trace where is the difference if there are 200+ fileds and thousads of reords.