in reply to How can I replace a single character (digit) in a file
pref1\t0\n
and you want to be able to change it to:
pref1\t1\n
without rewriting the entire file? If that is correct you can do this which is basically straight out of the Perl Cookbook. Note it uses quite a bit of memory if the file is going to be large.
-----------------my $pref = 'pref1'; open(FH, "+< $FILE") or die "Error opening $FILE: $!\n" my @lines = <FH>; seek(FH,0,0); my $count = 0; foreach my $l ( @lines ) { $count++; # Skip lines not matching $pref if( $line !~ /^$pref/ ) { next; } my ($pref, $num) =~ /^(.*?)\t(.*?)$/; $num = 1; $lines[$count] = "$pref\t$num\n"; } print FH @lines; truncate(FH, tell(FH)); close(FH);
Edit kudra, 2001-07-30 Changed pre tags to code tags
|
|---|