# For json.sh stringa: the text read on standard input as one JSON string (RFC 8259), quotes included, on one line. # # printf '%s' "$testo" | LC_ALL=C awk -f json-stringa.awk # # The lines are joined by a line feed («\n»); a line feed at the very end of the input is not kept. The quote, the # backslash and every control character below 0x20 are escaped; every other byte passes as it is (UTF-8 stays UTF-8, # which JSON allows). The text is read byte by byte under LC_ALL=C: fine for the notes of a release, a name, a tag. BEGIN { for (i = 1; i < 32; i++) escaped[sprintf("%c", i)] = sprintf("\\u%04x", i) escaped["\b"] = "\\b" escaped["\f"] = "\\f" escaped["\n"] = "\\n" escaped["\r"] = "\\r" escaped["\t"] = "\\t" escaped["\""] = "\\\"" escaped["\\"] = "\\\\" text = "" } { text = (NR > 1 ? text "\n" : "") $0 } END { out = "" n = length(text) for (i = 1; i <= n; i++) { c = substr(text, i, 1) out = out ((c in escaped) ? escaped[c] : c) } printf "\"%s\"\n", out }