Link to home
Start Free TrialLog in
Avatar of Moustaph Diawus
Moustaph Diawus

asked on

perl to compare 2 files

Hi
i do this perl script:

#! /usr/local/bin/perl
use File::Compare;         #Module de comparaison des fichier
 $fichier_1="fic1";
$fichier_2="fic2";
if (compare($fichier_1, $fichier_2) == 0)
{
  printf("%s et %s ont le même contenu",$fichier_1,$fichier_2);
}
else
 {
   printf("%s et %s sont différents",$fichier_1,$fichier_2);
 }

now i wish fic1 and fic like the argument for execution ( compare.pl  fic1 fic2); and
-Check that there are 2 parameters by looking at the length of @argv
-Print an error message if it is not.
-open both files, display an error message if a file does not exist.
and compare the two lists line by line and display a message if a difference
is detected.
If there was no difference indicate that the files are similar.

Thanks
Avatar of Jim Riddles
Jim Riddles
Flag of United States of America image

Have a look at this Perl module: http://search.cpan.org/~neilb/Text-Diff-1.45/lib/Text/Diff.pm.

It should do what you are asking for, however, you will need to check that the file exists before feeding into this module as it croaks on file not found.
Avatar of Moustaph Diawus
Moustaph Diawus

ASKER

the file name is integrate in this module, but i wish to put file1 and file2 like the argument ( diff file1 file2);
and the result should be: file1 equal file2; or file1 and  file2 are different.

thanks
Filenames are passed as arguments in the module that I specified. Did you check out the link?
yes, fic1 and fic2 are the same file but answer say: see attachment for file content and answer.
also the answer is same if put argument or not.

Thanks
perl.docx
Can you show the code of comptest1.pl?
Could there be non-printing characters in fic1 or fic2 that you are not seeing in the cat output?
What is in fic3?
What happens if you do comptest1.pl fic1 fic1?
this is the code: comptest1.pl

#! /usr/bin/perl
use File::Compare;
open (IF,"<fic1");
open (IF2,"<fic2");
my(@fic1) = <IF>;
my(@fic2) = <IF2>;
my ($i);
my ($j);
while (@fic1)
{
 $i=shift @fic1;
 $j= shift @fic2;
 chomp($i);
 chomp($j);
 #print "$i\n";
 #print "$j\n";
 if (compare("$i","$j") == 0) {
 print "$i $j are equal\n";
 }
 else
 {
 print "$i $j are not equal\n";
 }
}
close(IF);
close(IF2);

the fic3 is juste fic1 with 2 different character

[tester@DC1 ~]$ perl comptest1.pl fic1 fic2
123456789 123456789 are not equal
abcdefgh abcdefgh are not equal

thanks
if (compare("$i","$j") == 0) {

Open in new window

File::Compare::compare wants two filenames
you probably don't have any files named "123456789" or "abcdefgh" so it probably encounters an error and returns -1

If you just want to see if $i and $j contain the same string, you can say
if ( $i eq $j ) {

Open in new window

If you want to use File::Compare::compare to compare the files "fic1" and "fic2" you could say
if( compare("fic1","fic2") == 0 ){  print "files are equal\n"; }

Open in new window

i change the program:
#! /usr/bin/perl
use File::Compare;
open (IF,"<fic1");
open (IF2,"<fic2");
my(@fic1) = <IF>;
my(@fic2) = <IF2>;
my ($i);
my ($j);
while (@fic1)
{
 $i=shift @fic1;
 $j= shift @fic2;
 chomp($i);
 chomp($j);
 #print "$i\n";
 #print "$j\n";
if( compare("fic1","fic2") == 0 ){  print "files fic1 and fic2 are equal\n"; }
#if (compare("$i","$j") == 0) {
#print "$i $j are equal\n";
 else
 {
 print "$i $j are not equal\n";
 }
}
close(IF);
close(IF2);

the result is:

[root@DC1 tester]# perl comptest1.pl
files fic1 and fic2 are equal
files fic1 and fic2 are equal
[root@DC1 tester]#

i don't no why files fic1 and fic2 are equal is repeat

Thanks
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I  try to out the loop but the answer no change, can you give me more details to do this

Thanks