open(my $fh, "<", $ARGV[0]) or die "\n$0 Error => $^E\n"; my (@records,$rec); while (<$fh>) { unless (/^\d+\s+/) { $rec .= $_; next } push @records, $rec if $rec; $rec = $_ } push @records, $rec if $rec; close $fh; # Now each item of @records has the lines you're looking for