my @cqafiles = glob( "$cqapath/cqa_*" ); open( STATUS, '>', "cqapath/CQA_STATUS" ) or die "Can't open file $cqapath/CQA_STATUS for writing." . "\n$!"; foreach my $file ( @cqafiles ) { open my $handle, '<', $file or die "Can't open $file.\n$!"; FINDLINE: while( my $line = <$handle> ) { next unless $line =~ m/^CQA_STATUS/; $line =~ s/^[^,]+//; print STATUS $line; last FINDLINE; } } close STATUS or die "Can't close status file\n$!";