# shell command, assuming there are not tons of files in file_path: grep -h ^CQA_STATUS file_path/cqa_* > file_path/CQA_STATUS # but if there are tons of files, do it like this: find file_path -name 'cqa_*' | xargs grep -h ^CQA_STATUS > file_path/CQA_STATUS # (update: added carets where needed) #### 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;