Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2022-08-12 03:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?