use strict; use warnings; open TEST, "tests.txt" or die $!; open OUTPUT, "> output1.txt" or die$!; my @file = grep/SUBSCRIBER/../NATIONAL/, ; my $line; foreach $line(@file) { if ($line=~m/A1/) { print OUTPUT @file; } } close TEST; close OUTPUT;