my $file = 'directory[1].txt'; open (FH, "$file") or die $!; #while (/(?<=>)([\w\s]+?) PRIMARY\s+SCHOOL/g ) { print $&,"\n"}; my @schools; while (my $line = ) { if ($line =~ /(?<=>)([\w\s]+?)\sPRIMARY\s+SCHOOL/g) { push (@schools, "$1"); } } print "@schools\n";