If the part of the file you are going to be replacing is at the end, you can increase speed and efficiency by not writing to the disk until you get to the exact part of the file where you have made the change. Once you make a change, however, you'll have to write everything else from memory to the disk. This is basically because when you change a file, you change it's length. (If you are simply subsituting one letter for another, it's possible (though a bit foolhardy, IMO) to open it for read/write, find and make your changes, and close the file, as the size will remain the same) For a more normal, not-as-foolhardy case, use something like this:

## Open our file for reading and writing: open(MYFILE, "+< $myfile") or die "Could not open $myfile: $!\n"; ## File locking would be a Good Idea here ## Loop through the file one line at a time while(<MYFILE>) { if (/$foo/) { ## We have a match! my $tellme = tell(MYFILE)-length $_; ## See below my @baz = <MYFILE>; ## Slurp everything from that point forward unshift(@baz,$_); ## Throw our string back in ## Above, we stored the location in the file where the ## first line was that matched. Now we rewind to that point. seek(MYFILE,0,$tellme); ## The file from where we are (via seek) to the end of ## the file is now in memory, inside @baz for (@baz) { s/$foo/$bar/; print MYFILE $_; } ## Now we must cleanup our file, especially if the ## file has shrunk from our changes: truncate (MYFILE,tell(MYFILE)); last; ## Bail from the original MYFILE loop } }
This is kind of a pain (obviously) but really can save you memory if the file is large or your RAM is small. The above is also a bit verbose - you can remove a lot of the "$_" and "MYFILE"s from above - see the documentation on seek, truncate, and tell for more.


In reply to Re: The easiest way to substitute a part of the content of a file. by turnstep
in thread The easiest way to substitute a part of the content of a file. by MF

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.