in reply to Confusion with two-way pipes

Thank you everyone for your replies.

Integral, since foo represents the last step before writing the file, your simple solution will work best, right now.

Tall_man, thank you for the strongest theoretical solution (something I want to learn, because I may need it in the future).

Clairudjinn, the outside program foo (NOT a perl script) will do the final processing, and I need to get the data to that program.

Bsb, I will have to do this on hundreds of files, and I saw a number of recommendations against using system calls.