open( O, ">", "file_path/CQA_STATUS" ) or die "CQA_STATUS: $!"; for my $file ( ) { open( I, "<", $file ) or do { warn "$file: $!"; next }; while () { # suppose we only want the first occurrence from each file if ( /^CQA_STATUS/ ) { print O; last; } } close I; } close O;