To get to the heart of the matter, as it were, your principles: as I understand the relative efficiencies and strengths, one might argue that awk or sed might be the 'right tool for the job.'
Note that while I know how I would do this in Perl, there are already several good answers. I do not know, off the top of my head, how to do it in sed or awk.
I like computer programming because it's like Legos for the mind.
Comment on Re: Cleaning up a text file with compact regex