Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How can I find and delete a line from a file?

( #14498=categorized question: print w/ replies, xml ) Need Help??
Contributed by Cobra2411 on May 24, 2000 at 09:01 UTC
Q&A  > data formatting


Description:

I have a comma-separated data file with three columns, e.g.

Fruit,apple,red Sandwich,hamburger,rare Drink,soda,diet
I need a script to delete a line based on the first and second field. For example,
removeline.pl comma.file sandwich hamburger
would remove the line, but
removeline.pl comma.file sandwich soda
would not.

Answer: How can I find and delete a line from a file?
contributed by perlmonkey

Put simply, you'll have to read in the file, process it, and then save it back out.

my( $filename, $arg1, $arg2 ) = @ARGV; open IN, '<', $filename or die; my @contents = <IN>; close IN; @contents = grep !/^$arg1,$arg2,/, @contents; open OUT, '>', $filename or die; print OUT @contents; close OUT;

This will force the first and second field in the file to match exactly the first and second arguments that you pass in.

Answer: How can I find and delete a line from a file?
contributed by questor

Here's a command-line one-liner:

perl -p -i.bak -e "next if /^sandwich,hamburger,/" comma.file

You could wrap this in a batch script or shell alias to facilitate the substitution of parameter values.

Alternatively:

perl -n -i.bak -e "print unless /^sandwich,hamburger,/" comma.file
Answer: How can I find and delete a line from a file?
contributed by dimar

Another alternative is to use Tie::File. This lets you treat a file as a regular array variable. This is very convenient because it is easy to understand what is going on, as long as you are familiar with perl arrays.

use Tie::File; my( $file_name, $arg1, $arg2 ) = @ARGV; # open the file with tie tie my @file_lines, 'Tie::File', $file_name or die; # delete the first line shift @file_lines; # filter out lines containing 'sandwich' @file_lines = grep !/^$arg1,$arg2,/, @file_lines; # close the file with untie. # IMPORTANT: always untie when you are done! untie @file_lines or die "$!";

Update: Note that perl does not actually load the file into the array. It only appears to do so.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (2)
    As of 2014-09-20 07:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (157 votes), past polls