How would I remove a portion of a line if I find a match.

I have an array and a txt file

my @array = qw/file1.txt file2.txt/;

my txt file looks like this                               and need it to look like this

file10!date!time!file5.txt!location                   file10!date!time!file5.txt!location
file11!date!time!file1.txt!location                   file11!date!time!!location
file12!date!time!file2.txt!location                   file12!date!time!!location
file13!date!time!file4.txt!location                   file13!date!time!file4.txt!location

loop though @array, if I find a match in the txt file remove it.

my $file = test.txt
my @remove_array = qw/file1.txt file2.txt/;

open(FH, "$file") || die "can't open $file $!\n";
my @txt_array = <FH>;
close FH;

I'm not sure how to remove a portion of  @txt_array  if there is a match from @remove_array.
 
ckasparAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mjcoyneCommented:
#!/usr/bin/perl -w
use strict;

my $file = 'test.txt';
my @remove_array = qw/file1.txt file2.txt/;
my $i;
open(FH, "$file") || die "can't open $file $!\n";

while (<FH>) {
    for ($i = 0; $i < @remove_array; $i++) {
    s/$remove_array[$i]//;
    print $_;
    }
}
mjcoyneCommented:
Oops -- that won't work right.  Sorry.  Try it this way:

#!/usr/bin/perl -w
use strict;

my $file = 'test.txt';
my @remove_array = qw/file1.txt file2.txt/;
my ($i, $contents);
open (FH2, ">new_file.txt") or die;

    {
    local( $/, *FH ) ;
    open(FH, "$file") || die "can't open $file $!\n";
    $contents = <FH>
    }


    for ($i = 0; $i < @remove_array; $i++) {
    $contents =~ s/$remove_array[$i]//;
    }


    print FH2 $contents;
ozoCommented:
do you want to remove it from @txt_array or from the file?
do you want to remove it from all fields, or just the field between the time and location?
do you want to remove "file1.txt" from
file11!date!time!aafile1.txtbb!location
to look like
file11!date!time!aabb!location
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

ckasparAuthor Commented:
do you want to remove it from @txt_array or from the file?
From the file.

file11!date!time!aafile1.txtbb!location
to look like
file11!date!time!aabb!location

This would never be the case aafilebb, but if it was, I would remove the whole thing.
file11!date!time!file1.txt!location
to look like
file11!date!time!!location
wlfsCommented:
my @remove_array = qw/file1.txt file2.txt/;

open my $in, "<ee_01.txt";
open my $out, ">ee_02.txt";

(my $regex = join "|", @remove_array) =~ s/\./\\./g;
s/(?<=time!)(?:$regex)(?=!location)//g, print $out $_ while <$in>;

close $in;
close $out;
ckasparAuthor Commented:
Within the example, it may not be in the order. I need to scan the entire file, if I find an element remove it.
e.g.
one file may be          = file11!date!time!file1.txt!location
another line may be   = file12!date!file2.txt!location!time
another line may be   = file14!Tom!Dick!Harry!file1

my @array = qw/file1.txt file2.txt/;

my txt file looks like this                               and need it to look like this

file10!date!time!file5.txt!location                   file10!date!time!file5.txt!location
file11!date!time!file1.txt!location                   file11!date!time!!location
file12!date!time!file2.txt!location                   file12!date!time!!location
file13!date!time!file4.txt!location                   file13!date!time!file4.txt!location
ozoCommented:
do you mean that if @array contains "file1.txt" then
file11!date!time!aafile1.txtbb!location
should be changed to
file11!date!time!!location
?
if you had
aafile1.txtbb!file1.txt!file1.txt!file1.txt!file1.txt
would you change that to
!!!!


ckasparAuthor Commented:
OZO, your examples doesn't apply

any element in @array = qw/file1.txt file2.txt/; would be between the exclamation marks by themselves.

So if I see !file1.txt! or !file2.txt! some where within the file remove it.

A scenario with aafile1.txtbb!file1.txt!file1.txt!file1.txt!file1.txt wouldn't happen.





ozoCommented:
{my @re=map{qr/[^!]*\Q$_\E[^!]*/ @array;
 local @ARGV=qw(file1.txt);
 local $^I="";
 while( <> ){
   for $re ( @re ){ s/$re//g }
   print;
 }
}
ozoCommented:
{my @re=map{qr/(?:^|!)\Q$_\E(?:!|$)/ @array;
 local @ARGV=qw(file1.txt);
 local $^I="";
 while( <> ){
   for $re ( @re ){ s/$re// }
   print;
 }
}
ozoCommented:
any element in @array = qw/file1.txt file2.txt/; would be between the exclamation marks by themselves.

So if I see !file1.txt! or !file2.txt! some where within the file remove it.

Are you saying that a scenario with
file11!date!time!file1.txt!location
file11!date!time!file1.txt!location
file11!date!file1.txt!file2.txt!location
file1.txt!date!time!!location
file11!date!time!!file1.txt
won't happen?
ckasparAuthor Commented:
Where are the variable for the code? I tried this and it errored out.

my $file = 'test.txt';
my @array = qw/file1.txt file2.txt/;

{my @re=map{qr/[^!]*\Q$_\E[^!]*/ @array;
 local @ARGV=qw($file);
 local $^I="";
 while( <> ){
   for $re ( @re ){ s/$re//g }
   print;
 }
}

ozoCommented:
my $file = 'test.txt';
my @array = qw/file1.txt file2.txt/;

{my @re=map{qr/[^!]*\Q$_\E[^!]*/} @array; #this changes !aafile1.txtbb! to !!
 local @ARGV=($file);
 local $^I="";
 while( <> ){
   for $re ( @re ){ s/$re//g }
   print;
 }
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mjcoyneCommented:
Is there some reason why entry doesn't meet you needs?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.