in reply to edit text

You just have to buffer the last line you read and output it only after editing (if it needs to be edited):

#!/bin/perl -w use strict; my $last_line=''; while( <DATA>) { chomp; if( m{\bedit\b}) { # edit to taste $last_line .= ' # edited'; } print $last_line, "\n"; $last_line= $_; # buffer the line } print $last_line, "\n"; __DATA__ *********************** word1 word2 word4 word3 word4 word5 word6 word7 word8 word9 edit word10 word11 word12 word13 edit ************************

Note that this way you only have 2 lines in memory at all time, which can be useful if you are processing a hige file.