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

Posted on 2006-04-23
Last Modified: 2010-03-05
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
Question by:ckaspar
    LVL 8

    Expert Comment

    You will need to open the file, read the contents, do the match and write the updated data.
    LVL 25

    Accepted Solution

    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);
    LVL 5

    Expert Comment

    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 (<>) {

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

    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 = "";
    LVL 5

    Expert Comment

    I think the trouble is that the 'fileXY' can occur at other fields than $data[5] as well, e.g. 'file1' in line 4.
    LVL 25

    Expert Comment

    Exactly, he doesn't want to remove it if it does.  My script won't.
    LVL 5

    Expert Comment

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

    Author Comment

    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);
    do I need just to open the file?
    I don't see any code that takes care of the original file.
    LVL 25

    Expert Comment

    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 and your file was called file.txt:

       perl file.txt

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

      perl file.txt > updatedfile.txt

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

     perl -i.bak file.txt

    That will create a copy of your original file.txt called file.txt.bak and replace file.txt with the filtered version.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    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…
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    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…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now