in reply to Replace a line with a new one
-nuffin zz zZ Z Z #!perlopen (FILE,"external_file.txt"); open (TEMP,">external_file.txt.tmp"); my $i = 0; while (<FILE>){ # magical - puts every line in $_, until the end of fi +le is reached if ($i == 3){ # if $i is the 3rd line chomp $_; # remove the line break at the end of $_ $_ .= "my new line goes here\n"; # add the string } print TEMP $_; # print the line you just read to the temporary fil +e } continue { # performed at the end of every loop iteration $i++; } close FILE; close TEMP; unlink ("external_file.txt"); # incase rename does not clobber, delete + the original rename ("external_file.txt.tmp","external_file.txt");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Replace a line with a new one
by Donnie (Acolyte) on Oct 14, 2002 at 22:21 UTC | |
by nothingmuch (Priest) on Oct 14, 2002 at 22:39 UTC |