The biggest piece of the puzzle you need is the seek function (perldoc -f seek on your system or perlfunc:seek for a quick ref). While you can open files for both read and write access, I like to work with a scratch file. So here are the steps the way I'd do it:
open OLDFILE, $oldfilename or die "Can't open $oldfilename: $!\n"; # get length of first line my $start_pos = length(<OLDFILE>); # now read in next 11 lines for (1..11) { $_ = <OLDFILE>; } # we've got the 12th line in $_ # nab the 6 chars starting at the 50th position of $_ my $field = substr($_, 49,6); #rewind to the beginning of the second line seek(OLDFILE, $start_pos,0); open NEWFILE, "> $oldfilename.new" or die "Can't open $oldfilename.new for write: $!\n"; print NEWFILE "STring with $field in it\n"; # now write eatch line of OLDFILE INTO NEWFILE print NEWFILE while (<OLDFILE>); close OLDFILE; close NEWFILE; rename ($oldfilename, "$oldfilename.bak") or die "Can't move $oldfilename: $!\n"; rename ("$oldfilname.new", $oldfilenme) or die "Can't create new $oldfilename: $!\n";
Yeuch that's kinda clunky, but those are the steps.
HTH
In reply to Re: Text Processing
by arturo
in thread Text Processing
by Ixandantilus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |