by luizfelberti · 6y ago · 0 comments
If you try using awk or sed, you will need to figure out a magical way to escape whatever arbitrary value you are trying to replace, so you don't break sed's regex or awk's syntax.
M4 was one of the last things I tried, and it was an utter disaster. If any of M4's magical characters collides with the syntax of the file you're trying to template: tough luck. And there is no way to escape your file for M4 either, it's syntax doesn't support that.
I eventually discovered that Bash 4.4+ has some built-in functions for "dumb" string substitution[1], which is exactly what's missing from the default POSIX tools.
Since then I wrote this little wrapper around that functionality, as a simple, single-file, dependency-free Bash script. If you find something like that useful, it's on the link below:
[0] https://github.com/luizberti/templ
[1] https://www.tldp.org/LDP/abs/html/string-manipulation.html
it's part of `gettext`: https://www.gnu.org/software/gettext/manual/html_node/envsub...
https://www.gnu.org/software/gettext/manual/html_node/envsub...
The templating language looks similar to capability to that provided by templ.
As for perl, it does come "with the system" more often than Ruby, but also not as much as bash.
Even if it did, I reckon bash is something that nowadays people are more accustomed to than perl, and would consider it more of a no brainer.