DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with awk

Form-letter generation

The following program generates form letters, using a template stored in a file called form.letter:

   This is a form letter.
   The first field is $1, the second $2, the third $3.
   The third is $3, second is $2, and first is $1.
and replacement text of this form:
   field 1|field 2|field 3
   one|two|three
   a|b|c

The BEGIN action stores the template in the array line; the remaining action cycles through the input data, using gsub to replace template fields of the form $n with the corresponding data fields.

   BEGIN {	FS = "|"
                   while (getline <"form.letter")
                   line[++n] = $0
   }
   {       for (i = 1; i <= n; i++) {
                   s = line[i]
                   for (j = 1; j <= NF; j++)
                              gsub("\\$"j, $j, s)
                   print s
           }
   }

In all such examples, a prudent strategy is to start with a small version and expand it, trying out each aspect before moving on to the next.


Next topic: awk summary
Previous topic: History facility

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005