while () { my $line = $_; chomp($line); # -----A line starting with a '2' is a header and is left unchanged if ( $line !~ m/^2/ ) { # -----Locate postions 100 and 149 $juris1 = substr( $line, 99, 1 ); $juris2 = substr( $line, 148, 1 ); $juris1 = "9"; $juris2 = "Z"; # -----Generate the output string----- $output_line = substr( $line, 0, 98 ) . $juris1 . substr( $line, 100, 48 ) . $juris2 . substr( $line, 149, 61 ) . "\n"; print OUTPUT $output_line; last; } } #### while () { my $line = $_; chomp($line); # -----A line starting with a '2' is a header and is left unchanged if ( $line !~ m/^2/ ) { # -----Locate postions 100 and 149 $juris1 = substr( $line, 99, 1 ); $juris2 = substr( $line, 148, 1 ); $juris1 = "9"; $juris2 = "Z"; # -----Generate the output string----- $output_line = substr( $line, 0, 98 ) . $juris1 . substr( $line, 100, 48 ) . $juris2 . substr( $line, 149, 61 ) . "\n"; print OUTPUT $output_line; # last; # <-- this last exits the input loop, no more lines read. # Is that really what you want to do? # exit the loop after the first non - /^2/ - line? } else { print OUTPUT while ; } } #### # ----- Do as long as line doesn't start with '2'