Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Edit File in place

by RollyGuy (Chaplain)
on Aug 13, 2002 at 11:32 UTC ( #189742=perlquestion: print w/replies, xml ) Need Help??

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

At work I need to edit a file to add a comment character on some lines and also replace some multi-line strings with others. My first take is to slurp all of the lines of the file into an array, edit the lines one by one, and then write out the array back to the file. It seems to me that there should be a way to edit the file in place. So, how it that done?

Replies are listed 'Best First'.
Re: Edit File in place
by tommyw (Hermit) on Aug 13, 2002 at 11:41 UTC
Re: Edit File in place
by vroom (His Eminence) on Aug 13, 2002 at 13:56 UTC
    Another thing you may want to research is Tie::File it'll allow you to edit files in the way you want but won't require you to read the entire file into memory. This is a very nice thing if you're working on some rather large files. If the files are small this may be overkill.
Re: Edit File in place
by Bird (Pilgrim) on Aug 13, 2002 at 14:31 UTC
    The -i command line switch allows for editing files in place. It also has the ability to back up the original file.

    See perlrun


      This is not quite what the OP asked. From perlrun:

      -i[extension] specifies that files processed by the `<>' construct are to be edited in-place. It does this by renaming the input file, opening the output file by the original name, and selecting that output file as the default for print() statements. The extension, if supplied, is used to modify the name of the old file to make a backup copy, following these rules:
      Meaning that -i basically does the same 'trick': open for read, edit, write back.
        This is not quite what the OP asked.

        How? The original post asked for a way to edit a file in place, which this is. This is not the same solution that the original post presented, because it does not entail reading the entire file into an array, then writing it back out. It allows for line by line editing, as well as giving an option for renaming the original. Even if this was the same solution as the original, it's a much more concise form of it, which means you could write a quick one-liner using this version, as opposed to writing out a whole script using the alternative.


Re: Edit File in place
by spurperl (Priest) on Aug 13, 2002 at 13:23 UTC
    Reading the file, modifying and writing back is really not so bad of a solution. Especially if there is a lot to change, as seems to be in your case. IMHO, this provides a clean solution (you can have a backup of the original file easily, for exmaple), that is relatively easy to implement. So, unless you have a very special case in which you *must not* read-process-write the file, use this solution.
      I hadn't thought about the backup aspect of the edit process. I think that will be useful. Thanks for your comments.
Re: Edit File in place
by rinceWind (Monsignor) on Aug 14, 2002 at 10:36 UTC
    I would like to answer the underlying reason why editing the lines of text in-situ is not possible. Consider what the file actually is in terms of byte storage
    0000000 # ! / u s r / l o c a l / b i +n 043 041 057 165 163 162 057 154 157 143 141 154 057 142 151 15 +6 0000020 / p e r l - w \n u s e s t +r 057 160 145 162 154 040 055 167 012 165 163 145 040 163 164 16 +2 0000040 i c t ; \n p r i n t " H e l +l 151 143 164 073 012 160 162 151 156 164 040 042 110 145 154 15 +4 0000060 o W o r l d \ n " ; \n 157 040 127 157 162 154 144 134 156 042 073 012 0000074
    To change bytes in the middle may be possible, but to increase the length of lines would involve inserting bytes (hence shuffling the remainder forwards).

    Also, this is just how text files are implemented on one operating system (Unix). Others may use different line terminators, or implement in a completely diferent way, such as VMS, which stores the text file as a series of variable length records. Anything which relies of the Unix representation will potentially be non-portable.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2022-08-18 01:00 GMT
Find Nodes?
    Voting Booth?

    No recent polls found