in reply to multiple files to multiple files

I think the problem might be with your use of the $begin_flag and $end_flag flags: they are set to 0 at the beginning of your program, and then presumably set to 1 in the course of reading the first file (if the patterns are matched), but are never reset to 0, so that when you read the second file, $end_flag is 1 and you never enter into the print instruction for the subsequent files.

Note that the $begin_count variable is also never reset, and this is presumably not what you want. But we would need to see samples of your files to ascertain what you need to do.