[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 124
  • Last Modified:

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
0
ckaspar
Asked:
ckaspar
1 Solution
 
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
 
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
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!

 
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

Featured Post

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now