Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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

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


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, comma.file sandwich hamburger
would remove the line, but 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.


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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (6)
    As of 2020-09-25 18:01 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (140 votes). Check out past polls.