How to safely use the output of grep in a script?
... The output can be in any sort of data structure, as long as I can run code over it.
Shell scripts don't really have data structures. There are arrays, but that's about it—and it's not easy to get piped output into an array safely. (Filenames can contain newlines.)
The best way to run code over your files in a shell script is to just run the code over the files—not try to save the filenames for later use.
To do this, use find:
find somedir -type f -exec grep -q somepattern {} \; -exec somecommand {} \;
However, from reading your question more closely, it looks like you don't actually want to run code over your files, you just want to do some text processing on certain lines. In this case the GNU Grep option -z is probably what you want. That, and a knowledge of Sed or Awk, will handle your question.
It might be smart to change your file naming convention.