while (my $line = <$rfh>) { chomp $line; if ($line =~ m/^HEADER/) { my $number = 42; # change to whatever number you want to use $line .= $number; } if ($line =~ m/^REMARK/) { print {$wfh} "Extra line\n" # Change to whatever extra line you want } print {$wfh} $line, "\n"; }