A common problem in text processing is making sure that items that need to occur in pairs actually do so.
Most UNIX text editors include support for making sure that elements of C syntax such as parentheses and braces are closed properly. There's much less support for making sure that textual documents, such as troff source files, have the proper structure. For example, tables must start with a .TS macro, and end with .TE. HTML documents that start a list with <UL> need a closing </UL>.
UNIX provides a number of tools that might help you to tackle this problem. Here's a shell script written by Dale Dougherty that uses awk to make sure that .TS and .TE macros come in pairs:
You can adapt this type of script for any place you need to check for something that has a start and finish.
A more complete syntax checking program could be written with the help of a lexical analyzer like lex. lex is normally used by experienced C programmers, but it can be used profitably by someone who has mastered awk and is just beginning with C, since it combines an awk-like pattern-matching process using regular expression syntax, with actions written in the more powerful and flexible C language. (See O'Reilly & Associates' lex & yacc.)
And of course, this kind of problem could be very easily tackled in perl.
More Unix Power Tools
Copyright © 2009 O'Reilly Media, Inc.