compare 2 files

Hi,

I want to compare 2 files and get a new output that will contain the differences. Each file contain 5 fields (matricule, first name, last name, age, profession)
file1 is the original file. file2 should be synchronized with file1:I want to look for any change on file1 and want to apply these changes on file2.

#cat file1
10000;john;Trad;40;teacher
10001;georges;Hold;34;physician
10002;Catherina;Rick;36;doctor
10003;marc;bob;46;techician

#cat file2
10000;john;Trad;40;teacher
10001;georges;Hold;40;physician
10003;marc;Robert;46;programmer
10004;Maria;Roch;39;nurse

I us this script:

awk 'NR==FNR {f1[$0]=$0}
NR!=FNR {f2[$0]=$0}
END {
for(i in f1) if(!(i in f2)) print "Only in f1: " f1
[i]for(i in f2) if(!(i in f1)) print "Only in f2: " f2
[i]}' file1 file2

I get this result:

==============
Only in f1: 10001;georges;Hold;34;physician
Only in f1: 10003;marc;bob;46;techician
Only in f1: 10002;Catherina;Rick;36;doctor
Only in f2: 10004;Maria;Roch;39;nurse
Only in f2: 10003;marc;Robert;46;programmer
Only in f2: 10001;georges;Hold;40;physician

But it is not what I hope to get and obtain as result.
I want to get a result like that:

matricule:10001
change: modified
age:40

matricule:10002
change: deleted

matricule:10003
change: modified
lastname: Robert
profession: programmer

matricule:10004
change: added
firstname:Maria
lastaname:Roch
age:39
profession:nurse

Can someone help me to get this result with awk?

Thanks,

Imad77

You should use awk with join.
(But you allways need to keep files sorted by matricule key)

  join -1 1 -2 1 -a 1 -a 2 -t \; -o 1.1,1.2,1.3,1.4,1.5,2.1,2.2,2.3,2.4,2.5 file1 file2 \
| awk -F ';' \
'BEGIN{ split("matricule,firstname,lastname,age,prefession", HEAD, ","); }
{
  fld="";
  F2OFFSET=NF/2;
  ID=$1;
  CHANGE="modified";
  if($1=="")
  {
    ID=$(1+F2OFFSET);
    CHANGE="added";
  }
  else if($(1+F2OFFSET)=="")
  {
    CHANGE="deleted"
  }
  if(CHANGE!="deleted")
  {
    for(i=2;i<=F2OFFSET;i++)
    {
      if($i!=$(i+F2OFFSET))
      {
        fld=fld HEAD ": " $(i+F2OFFSET) "\n";
      }
    }
    if(fld=="")
    {
      CHANGE="";
    }
  }
  if(CHANGE!="")
  {
    print HEAD[1] ": " ID ;
    print "change: " CHANGE ;
    print fld ;
  }
} '

below perl script may help you a little.

sub ComArray{
        $n=0;
        $a=shift;
        $b=shift;
	@arr1=@$a;
	@arr2=@$b;
	for($i=0;$i<=$#arr1;$i++){
		if (!($arr1[$i] eq $arr2[$i])){
			$res[$n]=$i;
			$n++;
                }
		
		
        }
        return \@res;
}
@name=("matricule", "first name", "last name", "age", "profession");
open(FH1,"<file1");
while(<FH1>){
        @arr=split(";",$_);
        $hash{$arr[0]}=$_;
}
close(FH1);
open(FH2,"<file2");
while(<FH2>){
	$_=~tr/\n//d;
	@arr=split(";",$_);
        if(exists ($hash{$arr[0]})){
		$hash{$arr[0]}=~tr/\n//d;
		@temp=split(";",$hash{$arr[0]});
		$r=ComArray(\@arr,\@temp);
		@result=@$r;
		if($#result ge 0){
                        print "Update----> ",$name[0],":",$arr[0],"\n";
                        for($j=0;$j<=$#result;$j++){
                                print $name[$result[$j]],":",$arr[$result[$j]],"---->",$temp[$result[$j]],"\n";
                        }
                }
		delete($hash{$arr[0]});
        }
        else{
                print "Insert ---> ",$_,"\n";
        }
	print "\n";
}
close(FH2);
for $key (keys %hash){
	print "delete ---->",$hash{$key},"\n";
}