my $add_at_end = 1; while( my $ln = ) { if( $ln =~ s/str1/str2/ && $add_at_end ) { $add_at_end = 0; } print OUT $ln; } print OUT "text" $add_at_end;