in reply to CSV and regular expressions

What are you trying to do when you do while(ERROR){if($_ !~/STATUS CODE([0-9A-Za-z].*)VIP CODE/){... }}?There is nothing in $_ (at least not what you are expecting) since you are not reading from the ERROR-filehandle. To do that you must use <ERROR>. Then you read from the ERROR-filehandle, get the next line in $_ and you can test it. Now your regex fails everytime (and the test succeeds because you use !~) and you print the header file again and again.

The easiest way to deal with your program is to make the ERROR-file by hand (just typing the header-line in a text-editor or so) and never, ever write the header to it, just the lines you extracted from the other files.

Sometimes automation just gets in the way.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law