use strict; my $find = '^(H0|HT)'; open (NEW, ">", "OUTPUT.txt" ) or die "could not open:$!"; open (FILE, "<", "INPUT.txt") or die "could not open:$!"; while () { print NEW if /^(.{32}$find)/; } close (FILE); close (NEW); #### ------------------- INPUT FILE: JANE, DOE HT000000000 123 50 70 80 JOHN, DOE JR H0000000000 123 50 70 80 WILLIAM, DOE HT000000000 123 50 70 80 ------------------- EXPECTED OUTPUT FILE: HT000000000 123 50 70 80 H0000000000 123 50 70 80 HT000000000 123 50 70 80