#!usr/bin/perl -w use strict; use warnings; open (DATA, "test.txt") || die "Unable to open file: $!"; open (OUT, ">testout.txt") || die "Cannot create out file: $!"; my $line = ; my $type = substr ($line,0,2); # match first two characters in each line NEWLINE: while () { chomp; # remove terminating newline if ($type eq "10") { # matches all record 10 print OUT "\n"; print OUT "$line\n"; last if ($type ne "10"); } # loop adds blank row above record 10 elsif ($type ne "10") { # matches all non record 10 print OUT "$line\n"; last if ($type eq "10"); } next NEWLINE; # process next line } # end while close (DATA) || die "Unable to close file: $!"; close (OUT) || die "Unable to close file: $!";