Skip to content

Instantly share code, notes, and snippets.

@juster
Created September 2, 2015 22:31
Show Gist options
  • Save juster/11d2cbc7d11f84e481c1 to your computer and use it in GitHub Desktop.
Save juster/11d2cbc7d11f84e481c1 to your computer and use it in GitHub Desktop.
Tiny awk web template
function die (msg)
{
printf "%s:%d %s\n", FILENAME, FNR, msg > "/dev/stderr"
exit 1
}
BEGIN { FS="{!}"; OFS="" }
NF > 0 && NF % 2 == 0 { die("Unbalanced {!} tokens.") }
NF > 2 {
for (i=2; i<=NF; i+=2) {
if (!match($i, "^[A-Za-z]+$")) {
die("Invalid variable name: \"" $i "\"")
}
if (ENVIRON[$i] == "") {
die("Undefined variable: \"" $i "\"")
}
$i = ENVIRON[$i]
}
}
{ print }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment