ozzy ozzy - 1 month ago 8
Perl Question

How can I remove the pattern that are matched from original file

How can I remove the matched pattern that I print in output file from the original file. I used below script to print out all matched into the FILE6

open FILE4,'<RM' or die $!;
open FILE5,'<OR' or die $!;
open FILE6, '>Compare3.txt' or die $!;

my @array3 = <FILE4>;
my @array4 = <FILE5>;

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




Based on above coding, I print out the matched data from 2 file to the output file which is FILE6. So how to modify this code to remove matched pattern that print out in FILE 6 from original file which is FILE4 and FILE5. So that FILE4 and FILE5 only leave the pattern/data that are not matched.

Answer
use strict;
use warnings;

my $rm_qfn  = 'RM';
my $or_qfn  = 'OR';
my $out_qfn = 'Compare3.txt';

open(my $rm_fh, '<', $rm_qfn)
    or die("Can't open \"$rm_qfn\": $!\n");
open(my $or_fh, '<', $or_qfn)
    or die("Can't open \"$or_qfn\": $!\n");

open(my $out_fh, '>', $out_qfn)
    or die("Can't create \"$out_qfn\": $!\n");
open(my $out_rm_fh, '>', "$rm_qfn.tmp")
    or die("Can't create \"$rm_qfn.tmp\": $!\n");
open(my $out_or_fh, '>', "$or_qfn.tmp")
    or die("Can't create \"$or_qfn.tmp\": $!\n");

chomp( my @ors = <$or_fh> );

my @matched_ors;
while (my $rm = <$rm_fh>) {
    chomp($rm);
    my $matched_rm = 0;

    if (my ($all_reg) = $rm =~ /\S+ \d+ (\S+)/) {
        for my $or_idx (0..$#ors) {
            my $or = $ors[$or_idx];
            if ($or =~ /\b\Q$all_ref\E\b/i) {
                ++$matched_rm;
                ++$matched_ors[$or_idx];
                print($out_fh "$rm$or\n");
            }
        }
    }

    if (!$matched_rm) {
        print($out_rm_fh "$rm\n");
    }
}

close($rm_fh);
close($or_fh);

for my $or_idx (0..$#ors) {
    if (!$matched_ors[$or_idx]) {
        my $or = $ors[$or_idx];
        print($out_or_fh "$or\n");
    }
}

close($out_rm_fh);
close($out_or_fh);

rename("$rm_qfn.tmp", $rm_qfn)
   or die("Can't rename \"$rm_qfn.tmp\" to \"$rm_qfn\": $!\n");
rename("$or_qfn.tmp", $or_qfn)
   or die("Can't rename \"$or_qfn.tmp\" to \"$or_qfn\": $!\n");