I'm attempting to use awk to do some simple templating. I have a "template" file that looks like this:
{
"Thing": {
"Code": [
#include_code
]
}
}
I'm using the awk program below to replace the #include_code line with the contents of a file, except with every line wrapped in double-quotes and ending the line with a comma (to make a valid JSON list in my output).
#!/usr/bin/awk -f
! /#include_code/ { print $0 }
/#include_code/ {
while(( getline line<"test_file.js") > 0 ) {
print "\"" line "\","
}
}
where test_file.js is:
index.handler = (event, context) => {
return true;
}
My problem is that I don't want to print the very last comma, but I'm not sure how to prevent that comma being printed. To be explicit here's the output:
{
"Thing": {
"Code": [
"index.handler = (event, context) => {",
" return true;",
"}", <--- I don't want this comma...
]
}
}
While I'd like an answer that does this with awk (since I'm trying to learn it). I'll be happy with an answer that points me to a different tool for templating that you would recommend I use instead.