?
Solved

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

Posted on 2006-03-21
14
Medium Priority
?
167 Views
Last Modified: 2008-02-26
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.
 
0
Comment
Question by:ckaspar
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 17

Expert Comment

by:mjcoyne
ID: 16252910
#!/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
 
LVL 17

Expert Comment

by:mjcoyne
ID: 16252974
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
 
LVL 85

Expert Comment

by:ozo
ID: 16253131
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ckaspar
ID: 16254176
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
 
LVL 5

Expert Comment

by:wlfs
ID: 16254350
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
 

Author Comment

by:ckaspar
ID: 16254558
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
 
LVL 85

Expert Comment

by:ozo
ID: 16254707
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
 

Author Comment

by:ckaspar
ID: 16254772
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
 
LVL 85

Expert Comment

by:ozo
ID: 16254790
{my @re=map{qr/[^!]*\Q$_\E[^!]*/ @array;
 local @ARGV=qw(file1.txt);
 local $^I="";
 while( <> ){
   for $re ( @re ){ s/$re//g }
   print;
 }
}
0
 
LVL 85

Expert Comment

by:ozo
ID: 16254800
{my @re=map{qr/(?:^|!)\Q$_\E(?:!|$)/ @array;
 local @ARGV=qw(file1.txt);
 local $^I="";
 while( <> ){
   for $re ( @re ){ s/$re// }
   print;
 }
}
0
 
LVL 85

Expert Comment

by:ozo
ID: 16254815
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
 

Author Comment

by:ckaspar
ID: 16254849
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
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 16254871
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
 
LVL 17

Expert Comment

by:mjcoyne
ID: 16256659
Is there some reason why entry doesn't meet you needs?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question