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.
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
file11!date!time!file1.txt
file12!date!time!file2.txt
file13!date!time!file4.txt
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.
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;
#!/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;
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.t xtbb!locat ion
to look like
file11!date!time!aabb!loca tion
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.t
to look like
file11!date!time!aabb!loca
ASKER
do you want to remove it from @txt_array or from the file?
From the file.
file11!date!time!aafile1.t xtbb!locat ion
to look like
file11!date!time!aabb!loca tion
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
From the file.
file11!date!time!aafile1.t
to look like
file11!date!time!aabb!loca
This would never be the case aafilebb, but if it was, I would remove the whole thing.
file11!date!time!file1.txt
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;
open my $in, "<ee_01.txt";
open my $out, ">ee_02.txt";
(my $regex = join "|", @remove_array) =~ s/\./\\./g;
s/(?<=time!)(?:$regex)(?=!
close $in;
close $out;
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!loca tion!time
another line may be = file14!Tom!Dick!Harry!file 1
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
e.g.
one file may be = file11!date!time!file1.txt
another line may be = file12!date!file2.txt!loca
another line may be = file14!Tom!Dick!Harry!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
file11!date!time!file1.txt
file12!date!time!file2.txt
file13!date!time!file4.txt
do you mean that if @array contains "file1.txt" then
file11!date!time!aafile1.t xtbb!locat ion
should be changed to
file11!date!time!!location
?
if you had
aafile1.txtbb!file1.txt!fi le1.txt!fi le1.txt!fi le1.txt
would you change that to
!!!!
file11!date!time!aafile1.t
should be changed to
file11!date!time!!location
?
if you had
aafile1.txtbb!file1.txt!fi
would you change that to
!!!!
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!fi le1.txt!fi le1.txt!fi le1.txt wouldn't happen.
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!fi
{my @re=map{qr/[^!]*\Q$_\E[^!] */ @array;
local @ARGV=qw(file1.txt);
local $^I="";
while( <> ){
for $re ( @re ){ s/$re//g }
print;
}
}
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;
}
}
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!file 2.txt!loca tion
file1.txt!date!time!!locat ion
file11!date!time!!file1.tx t
won't happen?
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
file11!date!time!file1.txt
file11!date!file1.txt!file
file1.txt!date!time!!locat
file11!date!time!!file1.tx
won't happen?
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;
}
}
my $file = 'test.txt';
my @array = qw/file1.txt file2.txt/;
{my @re=map{qr/[^!]*\Q$_\E[^!]
local @ARGV=qw($file);
local $^I="";
while( <> ){
for $re ( @re ){ s/$re//g }
print;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Is there some reason why entry doesn't meet you needs?
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 $_;
}
}