Re: FAQ 5.2 How do I change, delete, or insert a line in a file, or append to the beginning of a file?



On 28 Dec 2006, jgibson@xxxxxxxxxxxxxxxxx wrote:

In article <ngkd64-fr8.ln1@xxxxxxxxxxxxxxxxxxx>, PerlFAQ Server
<brian@xxxxxxxxxxxxxx> wrote:


5.2: How do I change, delete, or insert a line in a file, or append to the
beginning of a file?


To change only a particular line, the input line number, $., is useful.
Use "next" to skip all lines up to line 5, make a change and print the
result, then stop further processing with "last".

while( <$in> )
{
next unless $. == 5;
s/\b(perl)\b/Perl/g;
print $out $_;
last;
}

This example results in a single line being written to the output file.
Is that is what is intended by "To change only a particular line"?

Well, that's what the documentation says right before the code, albeit
unclearly :)

Maybe it should be changed to

'The input line number, $., can be used to only change a particular
line. In the following example, we use "next" to skip (without
printing) all the lines up to line 5, make a change to line 5 and
print the result, then use "last" to stop processing by exiting the
loop.'

Ted
.