tarun28jain has asked for the wisdom of the Perl Monks concerning the following question:
I have a log file with some numeric data like
abc.log 12 15 17
I want to update the value present at offset 3 i.e 15 in this case with a given value like 20. I had done something like this
write_log("12345"); sub write_log { my ($message) = @_; my $new_data = ''; my $header_fields_value = 0; my $length = length $message; open (MYFILE, '>>', 'abc.log'); $header_fields_value = read_line(3); $new_data = $header_fields_value+$length+2; $header_fields_value =~ s/$header_fields_value/$new_data/g; seek(MYFILE,3,0); print MYFILE $header_fields_value; close (MYFILE); } sub read_line { my ($offset) = @_; open (MYFILE1, '<', $filename); seek(MYFILE1,$offset,0); my $line = <MYFILE1>; close (MYFILE1); return($line); }
here it is reading the value stored at offset 3 using read_line function. But modified value is being appended at the end of the file. I need it to replace the line present at offset 3.
Please help me in understanding what am i doing wrong or if it could be done in some easier way.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Replace a text in file at given offset with other text
by roboticus (Chancellor) on Feb 04, 2014 at 11:55 UTC | |
by tarun28jain (Initiate) on Feb 05, 2014 at 04:12 UTC | |
|
Re: Replace a text in file at given offset with other text
by DrHyde (Prior) on Feb 04, 2014 at 12:08 UTC | |
by tarun28jain (Initiate) on Feb 05, 2014 at 04:11 UTC | |
|
Re: Replace a text in file at given offset with other text
by petdance (Parson) on Feb 04, 2014 at 17:59 UTC |