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?
 
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;
 }
}
0
 
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 $_;
    }
}
0
 
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;
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
0
 
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
0
 
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;
0
 
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
0
 
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
!!!!


0
 
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.





0
 
ozoCommented:
{my @re=map{qr/[^!]*\Q$_\E[^!]*/ @array;
 local @ARGV=qw(file1.txt);
 local $^I="";
 while( <> ){
   for $re ( @re ){ s/$re//g }
   print;
 }
}
0
 
ozoCommented:
{my @re=map{qr/(?:^|!)\Q$_\E(?:!|$)/ @array;
 local @ARGV=qw(file1.txt);
 local $^I="";
 while( <> ){
   for $re ( @re ){ s/$re// }
   print;
 }
}
0
 
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?
0
 
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;
 }
}

0
 
mjcoyneCommented:
Is there some reason why entry doesn't meet you needs?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.