Link to home
Start Free TrialLog in
Avatar of ckaspar
ckaspar

asked on

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
Avatar of jhurst
jhurst

You will need to open the file, read the contents, do the match and write the updated data.
ASKER CERTIFIED SOLUTION
Avatar of clockwatcher
clockwatcher

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
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 = "";
@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.
Exactly, he doesn't want to remove it if it does.  My script won't.
> ...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.
Avatar of ckaspar

ASKER

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