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:
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 In reply to Re: Using read/write to update a file
by McA
|
|