You will at least need to read the line that needs to be checked. This will probably require reading the file up to that point. However, it may be cheaper from a memory point of view to read the file to that point (not saving it), determine if the change is needed, then seek back to the beginning and process the file if the change is needed (no reason to close it in this case). Then you can:<bl>
  • Write the edited version to a temporary file
  • Rename or unlink the original file
  • Rename the temporary file with the original name
  • </bl> I was going to suggest the -i switch until I thought about it a bit more and realized that you probably don't want to always write if the editing is not necessary (and I don't know about the seekability of the <> input, either).

    In reply to Re: editing a file by bikeNomad
    in thread editing a file by Anonymous Monk

    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.