#!/bin/sh # Copyright 2020 Gero Treuner # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . if test "$#" != 3 then echo 'Usage: $0 PLACEHOLDER FILEINSERT FILETRANSFORM Transforms a file by inserting a JSON string encoded file at placeholders. Arguments #1 placeholder string #2 file what content to insert at placeholder positions #3 file with placeholders to transform' test "$1" = '-h' -o "$1" = '--help' exit $? fi exec /usr/bin/gawk -F "$1" -v insertfile="$2" -- ' BEGIN { savedORS = ORS for (i = 0; i < 32; ++i) ctrlchars[sprintf("%c", i)] = sprintf("\\u%04X", i) } { if (NF > 1) { ORS = "" print $1 for (i = 2; i <= NF; ++i) { ORS = "\\n" while (0 < getline line < insertfile) { gsub(/["\\]/, "\\\\&", line) if (match(/[\b\t\n\f\r]/, line)) { gsub(/\b/, "\\b", line) gsub(/\t/, "\\t", line) gsub(/\n/, "\\n", line) gsub(/\f/, "\\f", line) gsub(/\r/, "\\r", line) } if (pos = match(line, /([\000-\037])/, a)) { ORS = "" do { print substr(line, 0, pos - 1) print ctrlchars[a[1]] line = substr(line, pos + 1) } while (pos = match(line, /([\000-\037])/, a)) ORS = "\\n" } if (RT == "") ORS = "" print line } close(insertfile) ORS = i == NF ? savedORS : "" print $i } } else { print } } ' "$3"