Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by questor (Acolyte)
on May 26, 2000 at 22:03 UTC ( [id://15054]=note: print w/replies, xml ) Need Help??


in reply to How can I find and delete a line from a file?

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

Replies are listed 'Best First'.
Re: Answer: How can I find and delete a line from a file?
by slvrstn (Initiate) on Oct 28, 2008 at 02:00 UTC
    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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found