Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi,

you must be aware that a file is a stream of bytes. There is no inherent concept of a record or line. A line ends where a byte representing a Newline (UNIX-like) or two bytes representing ASCII-Carrige-Return and Newline are found.

So, when you have a substitution which makes the line longer, you need more bytes between the first and the one markin the end. That means you have to move the content or the whole rest of the file towards the end. If the substitution makes the line smaller you have to shift the whole rest of the file towards the beginning of the file.

Only when you know that the substitution will NOT change the length of a line you know that the line before the substitution and after the substitution will require the same space, you could implement something like you want.

In general you always produce a copy of the original file with all substitutions and delete the original afterwards.

When you're short of space you can split the original file into X parts having a certain count of lines, make the substitution with copy in that part and concatenate all resulting files afterwards. Something like that on a Unix shell:

split -l 1000 myfile myfile. for file in myfile.* do perl -i -pe 's/3/2/g' $file done cat myfile.* > newfile rm myfile myfile.*

where -l 1000 splits every 1000 lines and perl -i -pe 's/3/2/g' $file substitutes 3 by 2.

UPDATE: That is nonsense. With my example you copy the whole file in smaller pieces and gain nothing. I shouldn't talk to my children while answering questions here. Sorry.

Regards
McA


In reply to Re: Using read/write to update a file by McA
in thread Using read/write to update a file by PackerX

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 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?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found