If you will provide some detailed, real-world, examples of your source data (attach your file as a zip if you like) and exactly what you want the results to be, both in Code Blocks (or the actual file) along with the details of what text you want to extract, and what text you want to count, I'll take a look at it.
I don't know anything about grep, so I can't even read your code.