You could take a look at SQAWK (
https://github.com/dbohdan/sqawk) which is a Tcl implementation of an AWK-like program. Maybe you can get ideas from there or tweak it to work as a package instead of calling it externally as a CLI program.
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)