Sounds like a sed and awk project. Unfortunately, I’m rusty. An experience Unix guy would just write the command in a flash.
30 years ago I could whip up a lex and yacc parser for this in short order. Today... whats that...