use strict; use warnings; my $find = '^(H0|HT)'; my $header; open (NEW, ">", "OUTPUTTEST.txt" ) or die "could not open:$!"; open (FILE, "<", "NEWFILETEST.txt") or die "could not open:$!"; while () { if (/($find)/) { $header = $1; } elsif (/^\s{38}\S*/) { print NEW "$header\t", $_; } } close (FILE); close (NEW)