← Back to articles

Perl: Modify certain line number in text-file

A tiny Perl snippet that rewrites a specific line number in a text file — written up as pseudocode for a previous post.

Tedd
Just wrote up a small pseudocode for a previous post here.
[sourcecode type="php"]

open (IF, "infile.txt");
open (OF, "outfile.txt");
 while (my $line = <IF>) {
   $count++;
   if ($count == 10) {
     # Modify
     print OF "somethingelse\\\r\\
";
   } else {
     # Don't modify
     print OF $line;
   }
 }
close(OF);
close(IF);

[/sourcecode] 

Written by Tedd · Oslo, Norway

Follow the journal via RSS