21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

240 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>}function translate(from, to){return $0 = stranslate(from, to, $0)}# main programBEGIN {if (ARGC < 3) {print "usage: translate from to" > "/dev/stderr"exit}FROM = ARGV[1]TO = ARGV[2]ARGC = 2ARGV[1] = "-"}{}translate(FROM, TO)printWhile it is possible to do character transliteration in a user-level function, it is notnecessarily efficient, and we (the gawk authors) started to consider adding a built-in function.However, shortly after writing this program, we learned that the System V Release 4awk had added the toupper and tolower functions (see Section 8.1.3 [String-ManipulationFunctions], page 132). These functions handle the vast majority of the cases where charactertransliteration is necessary, and so we chose to simply add those functions to gawk aswell and then leave well enough alone.An obvious improvement to this program would be to set up the t_ar array only once,in a BEGIN rule. However, this assumes that the “from” and “to” lists will never changethroughout the lifetime of the program.13.3.4 Printing Mailing LabelsHere is a “real world” 5 program. This script reads lists of names and addresses and generatesmailing labels. Each page of labels has 20 labels on it, 2 across and 10 down. The addressesare guaranteed to be no more than 5 lines of data. Each address is separated from the nextby a blank line.The basic idea is to read 20 labels worth of data. Each line of each label is stored in theline array. The single rule takes care of filling the line array and printing the page when20 labels have been read.5 “Real world” is defined as “a program actually used to get something done.”

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!