Compare two files in Perl

Friday, December 28, 2012 , 0 Comments

I have 2 files, namely a.txt and b.txt. My task is to search all the strings of a.txt in b.txt. And if I have any match between the strings of a.txt and b.txt, then I want to print the line corresponding to that string and its next line from the file b.txt.
use strict;
use warnings;
my $line1;
my $line2;
my $fh;
my $fh1;
open $fh, "<", "b.txt" or die $!;
open $fh1, "<", "a.txt" or die $!;
my @b = <$fh>;
my @a = <$fh1>;
for (@b)
{
   $line1 = $_;
       for (@a)
       {
       $line2 = $_;
         if ($line1 =~ /^$line2$/)
         {
          print $line2 . " - is match\n";
         }
       }
}
exit;

0 comments: