ozzy ozzy - 1 month ago 7
Perl Question

how to print out match unique pattern from 2 different file using perl

I'am try to print out matched pattern from 2 file that have almost same data but have slightly different.
[below is the example file][1]

File 1
Orig : 109 ABCDEFG1
Orig : 119 ORANGECOLOUR


File 2
Clone : 108 abcd_efg_1
Clone : 113 orange_colour


Ouput expected
Orig : 109 ABCDEFG1
Clone : 108 abcd_efg_1

Orig : 119 ORANGECOLOUR
Clone : 113 orange_colour


So, based on the example above the different from 2 file is only the upper case/lower case and got some extra character "_". So how to print out matched data into 3rd file using perl



open FILE1,'<FILE1' or die $!;
open FILE2,'<FILE2' or die $!;
open FILE3, '>Output' or die $!;

my @array3 = <FILE1>;
my @array4 = <FILE2;


foreach $x (@array3) { if($x =~ /(\S+) (\d+) (\S+)/) { $temp_data = $3;
foreach $y (@array4) { if($y =~ /\b$temp_data\b/i) {print FILE3 "$x$y\n";}}
}}

close FILE1;
close FILE2;
close FILE3;





Above is the script that i have done. When I used this script, it only print out the pattern that are totally matched without consider the upper/lower case. .Below is the output when I used that code



Output file

Orig : Bluecolour
Clone : Bluecolour

Orig : Greycolour1
clone : Greycolour1




Xor Xor
Answer

If provided files and conditions are complete, following will work

open F1, 'f1.txt';
open F2, 'f2.txt';

my @f2 = <F2>;
foreach my $string1 (<F1>) {
    chomp($string1);
    my ($pattern1) = $string1 =~ m/(\S+)$/;
    foreach my $string2 (@f2) {
        chomp($string2);
        ($_) = $string2 =~ m/(\S+)$/;
        s/[^a-z0-9]//ig;
        print $string1 . "\n" . $string2 . "\n" if ($pattern1 =~ /^$_$/i);
    }
}

outputs:

Orig : ABCDEFG1
Clone : abcd_efg_1
Orig : ORANGECOLOUR
Clone : orange_colour

upd: you've added numbers to the input files, the script still works, no modification needed

outpus:

Orig : 109 ABCDEFG1
Clone : 108 abcd_efg_1
Orig : 119 ORANGECOLOUR
Clone : 113 orange_colour