Code sample: awk

The unix utility awk gets its name from the initials of its three creators, and not from the scabrous claims that it is awkward to use. This is a sample of my own awk coding. Ain't tellin' what it was for. It's only here as an example of what I do for hire (since I obviously couldn't make a living designing websites). Fellow awk aficionados will note that the coding style is not rigidly spartan; that's because in commercial work I favor a belt-and-gallus approach. Support calls at 0200 are not pleasant for anybody.

#!/bin/ksh
. . . various stuff in shell language . . .
echo "vprinter=${PRINTER:-ibm4029}\012 `cat ${SYSTEMFILE}`" | awk '
/.*/	{
	if ( $1 ~ /vprinter=/ ) {
		printer = substr( $1, ( index( $1, "=" ) + 1 ) );
	} else {
		outline=$0;
		if ( $1 ~ /\[Printers\]/ ) {
			printer_sec_found=1;
		} else if ( printer_sec_found == 1 ) {
			if ( $1 ~ /^\[.*\]/ ) {
				printer_sec_found=0;
			} else if ( $1 ~ /^.*=.*/ ) {
				if ( noted == 0 ) {
					outline=sprintf("%s=%s.ini",
						printer, printer );
					noted=1;
				} else outline="";
			};
		};
		if ( $1 ~ /^DefaultPrinter=.*/ ) {
			outline=sprintf("DefaultPrinter=%s", printer );
		};
		print outline;
	}
} ' > ${TMPFILE} && mv ${TMPFILE} ${SYSTEMFILE}
shell program continues to its end . . .

Return to language index.


A publication of the School of Tyrannus

Copyright © 1998, The School of Tyrannus
All rights reserved