Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Best way to edit a file?

by wackattack (Sexton)
on Apr 02, 2007 at 06:56 UTC ( #607764=perlquestion: print w/replies, xml ) Need Help??

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

What is the best way to quickly and easily open a file, edit a single line (with s/<old>/<new>/), and then save?

As of right now I will open the file and then save an edited version under a different name. Then I will delete the original file and then rename the new-edited version so it will take the place of the now deleted original file.

Is there a quicker way to do this? Or is the use of a temp file the only way to get this done?

Replies are listed 'Best First'.
Re: Best way to edit a file?
by MaxKlokan (Monk) on Apr 02, 2007 at 08:50 UTC
    Tie::File might help here. From the docs:
    Tie::File represents a regular text file as a Perl array.
    Each element in the array corresponds to a record in the
    file. The first line of the file is element 0 of the 
    array; the second line is element 1, and so on.
    The file is not loaded into memory, so this will work even for gigantic files.
    Changes to the array are reflected in the file immediately.

Re: Best way to edit a file?
by MonkE (Hermit) on Apr 02, 2007 at 13:03 UTC
    I recommend that you keep using your current approach (save as temp, delete original, and put temp in place of original). It's a very robust approach. It insures that you will always have a complete copy of the file, even if an error occurs during the save -- an error such as running out of disk space, etc. I would strongly caution against using any approach that destroys/overwrites/deletes the only copy of the file.
Re: Best way to edit a file?
by jesuashok (Curate) on Apr 02, 2007 at 07:10 UTC
    perl -i -pe 's/OLD/NEW/g' <file_name>
    there are lot of unix utilities are there to do the same. ( sed, awk , so on )

    hmmm ....let me think what did I said
Re: Best way to edit a file?
by DrHyde (Prior) on Apr 02, 2007 at 09:28 UTC
    echo ',s/foo/bar/g wq'|ed somefile

    Yes, that's a newline halfway through the command.

Re: Best way to edit a file?
by gasho (Beadle) on Apr 02, 2007 at 15:54 UTC
    perl -p -i.bak -e 's/ABC/DEF/' filename #will save original as .bak
    (: Life is short enjoy it :)
Re: Best way to edit a file?
by cdarke (Prior) on Apr 02, 2007 at 13:32 UTC
    You could open the file for read and write, find the line of interest, seek backwards to start of line, then overwrite it. BUT, I agree with MonkE.
    Text files lines are usually variable length. This means that for the single file approach to work the new line must be exactly the same length as the original.
Re: Best way to edit a file?
by leocharre (Priest) on Apr 02, 2007 at 17:00 UTC

    Imagine you're editing a site. Some email is spelled wrong so.. you gotta open up 40k lines of html code some stupid wysiwyg spat out. .. oh dear... well..

    find ~/public_html -type f -name "*.htm" -exec perl -i -pe 's/wrongo\@hi\.com/righto\@yo\.net/sig' '{}' \;

    I know of a case where we had a client come in, and they had hired someone full time for 6 months to fix things in hand made html that would have taken a posix monkey five seconds to do.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2023-05-30 18:28 GMT
Find Nodes?
    Voting Booth?

    No recent polls found