while(<$in>){ print $out $_; if( /word/ ){ print $out scalar <$in>, "APPEND"; } } close $in; close $out; rename $outfile, $infile;