Perl file comparison

Hi,
I'm a newbie in perl scripting. I want to compare first 18 characters of two files using perl and write output if they matches.

FileA;

835100000000000020000000001000000.000000000.000
835100000000000120000000001000000.000000000.000
835100000000000230000000001000000.000000000.000
835100000000000300000000001000000.000000000.000
835100000000000320000000001000000.000000000.000
835100000000000380000000001000000.000000000.000
835100000000000550000000001000000.000000000.000
835100000000000570000000001000012.000000001.000

FileB:

83510000000000002020100504000002.659000002.109000002.405000002.322
83510000000000012020100504000003.838000001.563000003.157000003.183
83510000000000019020100504000012.318000006.568000013.284000012.532
83510000000000020020100504000017.733000011.877000016.389000015.547
83510000000000023020100504000016.682000007.235000016.806000016.305
83510000000000030020100504000007.200000003.739000005.465000005.673
83510000000000032020100504000007.808000004.831000007.109000007.305
83510000000000038020100504000003.857000002.357000004.302000004.302
83510000000000050800000000000000.000000000.000000000.000000000.000
83510000000000051800000000000000.000000000.000000000.000000000.000
83510000000000055020100504000002.033000002.049000001.242000001.212
83510000000000057020100504000011.232000004.801000008.807000008.978

If the first 18 characters matches, output file B.

OutputFileB:

83510000000000002020100504000002.659000002.109000002.405000002.322
83510000000000012020100504000003.838000001.563000003.157000003.183
83510000000000023020100504000016.682000007.235000016.806000016.305
83510000000000030020100504000007.200000003.739000005.465000005.673
83510000000000032020100504000007.808000004.831000007.109000007.305
83510000000000038020100504000003.857000002.357000004.302000004.302
83510000000000055020100504000002.033000002.049000001.242000001.212
83510000000000057020100504000011.232000004.801000008.807000008.978

Any help would be much appreciated.

#!/usr/bin/perl

my $ainput = 'FileA' ;
my $binput = 'FileB' ;
my $boutput = 'OutputFileB' ;
my @temparr = () ;
my @mainarr = () ;

open(A,$ainput) or die "Error: $!\n" ;
open(B,$binput) or die "Error: $!\n" ;

my @aarr = <A> ;
my @barr = <B> ;

close(A) ;
close(B) ;

  foreach my $record (@aarr) {
  chomp $record ;
  my $sub = substr($record,0,18) ;
  @temparr = grep(/$sub/,@barr) ;
  push(@mainarr,@temparr) ;  }

open(BO,">$boutput") or die "Error: $!\n" ;
print BO @mainarr ;
close(BO) ;

Here's another way:

$
$
$ perl -lne '$ARGV eq "filea" ? $x{substr($_,0,18)}++ : defined $x{substr($_,0,18)} ? print : ""' filea fileb
83510000000000002020100504000002.659000002.109000002.405000002.322
83510000000000012020100504000003.838000001.563000003.157000003.183
83510000000000023020100504000016.682000007.235000016.806000016.305
83510000000000030020100504000007.200000003.739000005.465000005.673
83510000000000032020100504000007.808000004.831000007.109000007.305
83510000000000038020100504000003.857000002.357000004.302000004.302
83510000000000055020100504000002.033000002.049000001.242000001.212
83510000000000057020100504000011.232000004.801000008.807000008.978
$
$

tyler_durden

1 Like

another way,

# change the file names if requid.
open FILEA, '<filea';
open FILEB, '<fileb';

# get and store 
$hash{ substr($_, 0, 17) }++ while ( <FILEA> );

while ( <FILEB> ) {
    $part = substr ( $_, 0, 17 );

    print $_ if ( $hash{$part} );
}