#!/bin/sh -eu . ./config title="${1:-$TITLE}" content=$(cat) awk \ -v title="$title" \ -v content="$content" \ -v header="$TEMP_HEAD" \ -v footer="$TEMP_FOOT" \ ' function replace(str, pat, rep, out, parts, n, i) { n = split(str, parts, pat) out = parts[1] for (i = 2; i <= n; i++) out = out rep parts[i] return out } BEGIN { while ((getline line < header) > 0) hbuf = hbuf line "\n" close(header) while ((getline line < footer) > 0) fbuf = fbuf line "\n" close(footer) } { line = $0 line = replace(line, "{{TITLE}}", title) line = replace(line, "{{CONTENT}}", content) line = replace(line, "{{HEADER}}", hbuf) line = replace(line, "{{FOOTER}}", fbuf) print line }' "$TEMP_PAGE"