Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

delete data from file

by baxy77bax (Deacon)
on Aug 31, 2007 at 09:30 UTC ( [id://636280]=perlquestion: print w/replies, xml ) Need Help??

baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

Hi, my question is how to delete parts of a large txt file. Example: let say I have a large txt file (a whole novel), and every new paragraph starts with a tab:
Julie and Jolie were playing in the shadow when a large spider jomped on jolie and started dancing on her head Than something else happened , i don't know why or how...
now i want to delete the whole paragraphs that contain the word Julie in the first line. plus i do not want to create a new file but i want to save all changes in the same file. thank you! robert

Replies are listed 'Best First'.
Re: delete data from file
by moritz (Cardinal) on Aug 31, 2007 at 10:09 UTC
    You could try something like Tie::File and set the record separator to \n\t.
Re: delete data from file
by andreas1234567 (Vicar) on Aug 31, 2007 at 11:19 UTC
    use strict; use warnings; use English; $INPUT_RECORD_SEPARATOR = "\n\t"; while (<DATA>) { next if (m/Julie/); print; } __DATA__ Julie and Jolie were playing in the shadow when a large spider jomped on jolie and started dancing on her head Than something else happened , i don't know why or how... To my surprise Perl saved the day. Thank you Julie! Yet another reason to use Perl.
    $ perl 636280.pl Than something else happened , i don't know why or how... Yet another reason to use Perl.
    Perl inline replace is described in perlrun.
    --
    Andreas
Re: delete data from file
by Taulmarill (Deacon) on Aug 31, 2007 at 09:58 UTC
    Most of the time, there is no such thing as changing a file. Whenever you save changes in a file, the program you use will most likely create a new file and overwrite the old one with the new version. That's what i would recommend.
Re: delete data from file
by cdarke (Prior) on Aug 31, 2007 at 11:30 UTC
    You can't really. I will qualify that.
    If the data you wish to delete is at the end of the file then you could truncate the file, in other words you just move the position of EOF. I guess that is not the case.
    You could replace the characters that should be deleted with something like 0x00, but that is not guaranteed to be ignored by the code that will read it, unless you have control over that.
    The only practical way is to create a new file containing only those lines of interest, then rename it to the same name as the original.
Re: delete data from file
by perl_fan (Novice) on Aug 31, 2007 at 11:32 UTC
    hi there, you can use the perl-inline functionality. from the command line just type perl -pi -e 's/Julie and Jolie were playing in//ig' Just complete the paragraph in the above line. cheers, perl_fan
Re: delete data from file
by MidLifeXis (Monsignor) on Aug 31, 2007 at 18:04 UTC

    School is starting again. So in general terms...

    If you keep two file pointers (one read and one write), open the file read/write, and scan until your first match. Move the read file pointer to the end of the paragraph and the writer to the beginning of the paragraph. Now read from one and write to the other, skipping the write when the read runs into your key string. At the end, make sure you truncate to the end of the file.

    Update: Forgot to truncate

    --MidLifeXis

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found