in reply to CSV and regular expressions

Let me see if I can rephrase the problem:

If that's a fair description of the problem, then you could use the following logic (expressed here in pseudocode):

open a given input file open the output file read first line of input (this will be the csv header) look at the size of the output file if output file size is zero, write the csv header to output loop through remaining lines of input file write lines to output when appropriate
But if I've drawn the wrong conclusion about what you're trying to do, please clarify.