Code:
$ cat file1
D33963|BNS Default Swap|-261564.923909249|
D24484|BNS Default Swap|-53356.6868058492|
D24485|BNS Default Swap|-21180.9904679111|
D33965|BNS Default Swap|154181.478745804|
D24486|BNS Default Swap|-47413.0013193052|
D33966|BNS Default Swap|-154181.478745804|
D24487|BNS Default Swap|-63253.9807711966|
D33968|BNS Default Swap|-160521.81007754|
D24489|BNS Default Swap|-10584.4665849774|
S85801|BNS Swap|451309.300774646|
D33969|BNS Default Swap|118166.419991555|
$ cat file2
:E00277,48089,,,Trading,FALSE,,CAISSE,19189,AA,CAD
:D24485,48085,,,Trading,FALSE,,CASSE,19139,AA,CAD
:D2448,48083,,,Trading,FALSE,,CAIE,19029,AA,CAD
:D33963,48082,,,Trading,FALSE,,CAISSE,19149,AA,CAD,
:E00286CAP,48082,,,Trading,FALSE,,CAISSE,19149,AA,CAD
$ cat script.pl
use warnings;
use strict;
die qq[Usage: perl $0 <file1> <file2>\n] unless @ARGV == 2;
open my $fh1, "<", shift @ARGV or die qq[Error: Cannot open input file\n];
open my $fh2, "<", shift @ARGV or die qq[Error: Cannot open input file\n];
my (%file1);
while ( my $line = <$fh1> ) {
chomp $line;
my ($field1, $rest) = split /\|/, $line, 2;
$file1{ $field1 } = $line;
}
while ( my $line = <$fh2> ) {
chomp $line;
for ( keys %file1 ) {
if ( index( $line, $_ ) > -1 ) {
printf qq[%s%s\n], $file1{ $_ }, $line =~ s/,/|/gr;
last;
}
}
}
$ perl script.pl file1 file2
D24485|BNS Default Swap|-21180.9904679111|:D24485|48085|||Trading|FALSE||CASSE|19139|AA|CAD
D33963|BNS Default Swap|-261564.923909249|:D33963|48082|||Trading|FALSE||CAISSE|19149|AA|CAD|