replacing string with a comment(#) at start of that line

That is not as easy as you think. If you want to replace a line then you must replace every character in it. When you look at a text file in an editor like Notepad or vi(1) then it is showing a visual representation - the file is not really like that. For example:
abc defg hij
is really like this:
abc\ndefg\nhij\n
The detail varies between operating systems, but UNIX and Windows are similar. So, to replace a line with a comment you would have to:
1. Open the file for read and write
2. Read the file until you find the line you need
3. Move the current file position backwards to the start of the line you just read, using seek.
4. Overwrite each character with a '#' - no more and no less than the original line length.

It is not worth the effort! Instead it is easier to:
1. Open the original file for read
2. Open a new file for write
3. In a loop, read each record, then decide if it is to be written to the new file.
4. Write the record, or not, then read the next record, and so on until end-of-file.

In reply to Re: find & replace a string in perl by cdarke
in thread find & replace a string in perl by vr786

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.