Very briefly, regular expressions. I'd write this tool in Perl, but Tcl would do too since it has good regexp support.
There's a regexp tutorial at
http://analyser.oli.tudelft.nl/regex/index.html.en and information on Perl and Tcl at
http://www.perl.org/ and
http://www.tcl.tk/ respectively. Tcl is easier to pick up quickly if you don't know either language.
I don't think I'd try to tackle this with a shell script, although I know some die-hards who would.