Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by Cobra2411 (Initiate)
on May 24, 2000 at 09:01 UTC ( [id://14498]=perlquestion: print w/replies, xml ) Need Help??

Cobra2411 has asked for the wisdom of the Perl Monks concerning the following question: (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,
removeline.pl comma.file sandwich hamburger
would remove the line, but
removeline.pl comma.file sandwich soda
would not.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How can I find and delete a line from a file?
by perlmonkey (Hermit) on May 24, 2000 at 09:51 UTC

    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.

      If you know the exact line number you want to "delete," try this one-liner:
      perl -ni -e "print unless $. == $linenum" <filename>
Re: How can I find and delete a line from a file?
by questor (Acolyte) on May 26, 2000 at 22:03 UTC

    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
      This actually won't delete the line, since the implied print gets done as part of the continue portion of the loop, which happens even after a next.

      To delete, I think you'd need to substitute the matched line with null, but given that, I think the more obvious way is to switch to explicitly printing, like:

      perl -i -n -e 'print if not /^sandwich,hamburger,/i' comma.file
      This one-liner also takes advantage of the -i switch to edit the file in place, which seemed to be desired in the original question.
Re: How can I find and delete a line from a file?
by dimar (Curate) on Nov 13, 2004 at 03:50 UTC

    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.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How can I find and delete a line from a file?
by tempster09 (Initiate) on Mar 27, 2010 at 20:05 UTC
    You could do it on the fly:
    local @ARGV = ("input.file"); while(<>) { s/pattern//; # remove line matching pattern print; last if eof; }
    now you're done in one pass, but you need to include -i.bak in your perl command line.

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://14498]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-23 22:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found