perl比较两个目录中的文件内容是否相同

前端之家收集整理的这篇文章主要介绍了perl比较两个目录中的文件内容是否相同前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

#比较两个目录中的文件内容是否相同
#!/bin/perl
if($#ARGV < 0)
{
 print "usage:perl compare.pl dir1 dir2\n";
 exit(-1);
}
#my($rec,$ref) = @ARGV;

#!/bin/perl

my($lat1,$lat2)=@ARGV;

system ( "dir \/b \/s \/O:N  $lat1\\\*\.lat > lat1.list ");
system ( "dir \/b \/s \/O:N  $lat2\\\*\.lat > lat2.list ");

my $dif=1;

open(dlat1,"<lat1.list") or die "3\n";
open(dlat2,"<lat2.list") or die "4\n";
open(result,">result.log") or die "5\n";
$old = select result;
$|=1;   #immediately write flush(result);
select $old;


  my @alat1 = ();
    my @alat2 = ();
@alat1 = <dlat1>;
@alat2 = <dlat2>;
my $countout=@alat2;
my $count=0;
my $count2=0;
#select (select (result),$|=1)[0];
print (result  "result is\n");
 $|++;  # perl中的flush缓冲区   flush($tmp);
#close(result);
 
 
    for my $latline1 (@alat1)
    {
 #    open(result,">>result.log") or die "6\n";
     chomp $latline1;
     $count2=0;
    
     for my $latline2 (@alat2)
     {
      chomp $latline2;
      $dif = &find_fileindir("$latline1","$latline2");
      #$count2++;
      #print "$count:$count2 :dif is $dif\n" ;
      if($dif==0)
      {
       print (result  "$latline1 is equal $latline2\n");
        $|++;  # perl中的flush缓冲区   flush($tmp);
       #system ( "del /f /q  $latline2");
       
       
       last;
      }
      if($dif==1)
      {
       $count2++;
      }
      print "$count:$count2 :dif is $dif\n" ;
     }
     #print "count2 is $count2\n";
     #print "countout is $countout\n";
     if ($count2>=$countout)
     {
      print (result "$latline1 is not found\n");
       $|++;  # perl中的flush缓冲区   flush($tmp);
     }
 #    close(result);
     $count++;
     #print "$count\n";
    
   }

#$dif=&find_fileindir("$rec","$ref");

#print "dif is $dif";
close (dlat1);
close (dlat2);
close (result);

#下面函数比较两个文件内容是否相同
sub find_fileindir()
{
 
 local ($rec1,$ref1) = @_;

open(A,"$rec1") or die "1\n";
open(B,"$ref1") or die "2\n";
#open A,"a.txt";
#open B,"b.txt";
local @a=<A>;
local @b=<B>;
@a=map{split(/\s+/,$_)}@a;
#@a=map split @a;
@b=map{split(/\s+/,$_)}@b;
#@b=map split @b;
local $a=@a;
local $b=@b; local $diff=0;  #0代表相同,1代表不同 if($a<$b) {  $a=$b; } for (0..$a-1) {  if ($a[$_]==$b[$_])  { next;}  else  { $diff=1;return "$diff";} } #print "$diff\n"; return "$diff"; close(A); close(B); }

猜你在找的Perl相关文章