in reply to Replace a text in file at given offset with other text

tarun28jain:

A couple items:

Typically, if you want to make an edit in the middle of the file, you'll want to rewrite the entire file, unless your substitution text has the same number of bytes as the text it's replacing. (Can be complicated, especially when Unicode is involved.)

I'd suggest doing it a bit more like:

open my $INF, '<', $filename; open my $OUF, '>', $filename . ".new"; my @lines = <$INF>; $lines[2] = "12345\n"; print $OUF @lines;

However, reading the entire file into memory may be a problem, so you may want to process line-by-line instead.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Replace a text in file at given offset with other text
by tarun28jain (Initiate) on Feb 05, 2014 at 04:12 UTC

    Thanks for the reply. I got the solution to my specific problem from next reply