Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 38 ยท Projects and Practice

Project: A Template Engine and Mail Merge with AWK and sed

Build a tiny template engine: replace {{placeholders}} from a key=value file, support defaults and conditionals, generate sed scripts, and mail-merge CSV rows into letters.

Almost every program that sends emails, writes configuration files or generates web pages uses a template: text with holes, filled in from data. In this project you build three small template engines, each with a different technique, and finish with a mail merge that turns a CSV file into one personalised letter per row.

The first engine writes a sed script from your data. The second does the substitution inside AWK, with default values. The third adds a simple conditional. Together they show how far text tools can go before you need a real templating language.

Engine 1: generate a sed script

The simplest idea: for every key=value pair, emit s/{{key}}/value/g, then run sed with that script. The catch is escaping: a / or & in a value would break the command. We escape them first.

Bash A sed-based template engine

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Study the escaping: the value $120 & free shipping contains &, which in a sed replacement means "the matched text", so the script must contain \&. The AWK replacement "\\\\&" makes AWK write a backslash followed by the matched text (the & itself), producing \&. This layering (shell, AWK string, AWK replacement, sed) is the main reason to prefer the second engine: values containing backslashes are not handled here at all.

Engine 2: substitute inside AWK

Reading the data into an array and looking placeholders up is more robust: no escaping of the data is needed, because values never pass through a regex. We find each {{name}} with match and rebuild the line.

Bash An AWK template engine with defaults

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Three behaviours are worth noting. {{date|soon}} uses the text after | when the key is absent; {{nothing}} has no default, so it is flagged in the output as [MISSING nothing] and counted; and values containing &, / or $ pass through untouched because they are only concatenated. The NR == FNR idiom loads the data file first.

Engine 3: conditional sections

Many templates need "include this block only if...". A small convention: lines between {{#if key}} and {{/if}} are printed only when key is set and non-empty. A stack-free version supports one level, which is enough for most letters.

Bash Conditionals with a flag variable

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The skipping flag is the entire state: set at the #if line according to the data, cleared at /if, and while it is true all lines are dropped. Directive lines themselves are never printed.

The mail merge: one letter per CSV row

A mail merge applies one template to many records. Use the CSV header names as the keys, so a column called name fills {{name}}. Writing a file per row is as simple as print > filename.

Bash CSV to letters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The template is loaded once in BEGIN with getline, so each CSV row only loops over the stored lines. A zero amount is treated as "false" (no balance paragraph), as Cy Park's letter shows.

Limits and next steps

  • No escaping of HTML: if you render web pages, escape values before inserting them (a name like <script> would otherwise be copied through).
  • No loops or nested conditionals: for those use a real engine (Jinja, Mustache, Go templates, Blade).
  • Be careful when templates or data come from untrusted users. A template engine that evaluates code (eval, system) is a security hole; this one only substitutes text, which is much safer.

Ideas to extend it: {{name|upper}} filters, {{#unless key}}, include files with {{> file}}, and an error report listing all missing keys in the whole run.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is the AWK engine more robust than generating a sed script for arbitrary values?

    In the sed approach each value becomes part of a sed command and must be escaped; in AWK it is just data.
  2. 2. In the conditional engine, what does the skipping variable do?

    The flag is the whole state: set according to the data at the #if line and cleared at the closing line.
  3. 3. How does the mail merge name the keys for each row?

    NR == 1 saves the header names, and each later row fills row[header] = value.
  4. 4. What is a major limitation of this simple engine?

    It performs plain text substitution only: no escaping for HTML, loops or nesting.

Summary

  • A template engine substitutes {{key}} with values from data; AWK does it with match and substr.
  • Generating a sed script works but needs careful escaping; looking values up in an AWK array does not.
  • Defaults ({{key|default}}) and a skipping flag for {{#if}} blocks add real features in a few lines.
  • A mail merge combines CSV headers, a stored template and print > file.
  • Escape output for its target format (HTML) and never evaluate template text as code.

Next: AWK and sed Practice Exercises with Solutions