how do I search through a file and strip only things in a particular field, and build the file again?

I want to take each element in array @foo and only search field 5 in the file. if there is a match remove it, I'm not totally sure how to handle removing commons.  

my array
my @foo = qw/file1 file2 file3/;

lines     my $file                                  My result
1          x!y!z!t!r!file10!rr                      x!y!z!t!r!file10!rr                    
2          x!y!z!t!r!file1!rr                        x!y!z!t!r!!rr
3          x!y!z!t!r!file11,file2,file7!rr        x!y!z!t!r!file11,file7!rr
4          x!y!file1!t!r!file12!rr                  x!y!file1!t!r!file12!rr
5          x!y!z!t!r!file13,file3!rr               x!y!z!t!r!file13!rr
ckasparAsked:
Who is Participating?
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.

jhurstCommented:
You will need to open the file, read the contents, do the match and write the updated data.
0
clockwatcherCommented:
my @foo = qw(file1 file2 file3);
while (<>)
{
    @data = split /!/;
    @files = split(/,/,$data[5]);
   
    foreach $search (@foo)
    {
        @files = grep(!/^\Q$search\E$/, @files);
    }
   
    $data[5] = join(",",@files);
   
    print join("!",@data);
   
}
0

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
wlfsCommented:
my $foo = qr/(?:file1|file2|file3)/;
{
  local @ARGV=qw/name_of_file_to_modify.txt another_file_if_there_is_one.txt/;
  local $^I = '.bak';
  while (<>) {
    s/(,?)$foo(,?)/$1&&$2/ge;
  }
}

If you e.g. remove file1 and file12 of line 4, this will leave multiple "!" in like:
  x!y!!t!r!!rr
If you want all multiple ! to collapse into a single one like
  x!y!t!r!rr
just add the following line in the while loop after the exisiting s///ge:
  s/!{2,}/!/g;

By putting more than one filename into the local @ARGV array you can modify multiple files at once. The original content of the files is backuped by appending '.bak' to the filename. If you don't want a backup change $^I to
  local $^I = "";
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

wlfsCommented:
@clockwatcher:
I think the trouble is that the 'fileXY' can occur at other fields than $data[5] as well, e.g. 'file1' in line 4.
0
clockwatcherCommented:
Exactly, he doesn't want to remove it if it does.  My script won't.
0
wlfsCommented:
> ...and only search field 5 in the file.

aarghh. Didn't read this, sorry. Thanks clockwatcher for pointing out.
@ckaspar: please just ignore my post.
0
ckasparAuthor Commented:
clockwatcher -

How is the file being read in ?
Should I just create an array with my file?

my $file = "/home/file.txt";
open (FHI, "<"$file) || die "can't open $file$!\n":
@file = <$file>;
close (FHI);
or
do I need just to open the file?
I don't see any code that takes care of the original file.
0
clockwatcherCommented:
Pass the name of your file to the script.   The (<>) takes the argument list as a file list and opens each file passed in turn.  

Assuming you called the script removethem.pl and your file was called file.txt:

   perl removethem.pl file.txt


It will output to stdout.  So you could capture it with:

  perl removethem.pl file.txt > updatedfile.txt


If you want an inplace-edit, you would do this:

 perl -i.bak removethem.pl file.txt

That will create a copy of your original file.txt called file.txt.bak and replace file.txt with the filtered version.
0
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.

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.