in reply to Deleting and replacing lines in a file
When given the input file:use strict; open INPUT, "<myfile.txt" or die "Could not open input file: $!\n"; open OUTPUT, ">output.txt" or die "Could not open output file: $!\n"; my $count=0; while (<INPUT>) # Loop over lines of file { next unless ($. > 2); # Ignore first two lines s/do my/replacement/ if ($. == 3); print OUTPUT $_; # Print to output file. } close INPUT; close OUTPUT;
Line 1 Line 2 Line 3 do my Line 4it returns the output file:
Line 3 replacement Line 4Have fun exploring the wacky world of Perl!
CU
Robartes-
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Deleting and replacing lines in a file
by Thelonius (Priest) on Feb 14, 2003 at 14:00 UTC |