Sunday, January 25, 2015

Compare Files Perl

more file1.txt
1 12345
2 26789
1 4567
5 567
3 987

more file2.txt
5 525
7 728
1 670
1 4567
1 12345

Output
1    12345    1    12345
1    4567    1    4567


#!/usr/bin/perl
$file1 = 'file1.txt';
$file2 = 'file2.txt';

open (A, $file1) || die ("Could not open $file!");
open (B, $file2) || die ("Could not open $file!");
while ($Aline = )
{
        chomp $_;
        my @A = split(/\s+/,$Aline);
        seek B,0,0;
        while ($Bline = )
        {
                chomp $_;
                my @B = split(/\s+/,$Bline);
                        if($A[0] eq $B[0] and $A[1] eq $B[1] ) {
                            print "$A[0]\t$A[1]\t$B[0]\t$B[1]\n";
                        }
        }
}
close (B);
close (A);

No comments: