Link to home
Start Free TrialLog in
Avatar of ckaspar
ckaspar

asked on

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.
 
Avatar of mjcoyne
mjcoyne

#!/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 $_;
    }
}
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;
Avatar of ozo
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
Avatar of ckaspar

ASKER

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
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;
Avatar of ckaspar

ASKER

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


Avatar of ckaspar

ASKER

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.





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

ASKER

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;
 }
}

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is there some reason why entry doesn't meet you needs?