First, do this:
If you end up being able to do this with sed, and avoid awk, it will go faster.
But read those two man pages.
Then start with a simpler problem than your final one. Dink with it until it works. Then add complexity until you have what you want.
If, along the way, you have a detailed question about why what you wrote doesn't work, this is a really good way to ask about it. But post all
the details, including your sed or awk command, your input data, your output data, and any error message.