Note that newlines in values must be encoded as \n
in JSON. A JSON parser would decode these as real newlines. Insertin a literal newline in a value in a JSON file would result in a broken JSON document.
Using jo
(a tool for generating JSON output in the shell, with the correct encoding etc.):
awkresult='some string
with newlines
the end'
jo result="$awkresult"
This would result in the output
{"result":"some string\nwith newlines\nthe end"}
To pretty print:
jo -p result="$awkresult"
which results in
{
"result": "some string\nwith newlines\nthe end"
}
Redirect the output of jo
to a file to save the output, e.g.
jo result="$awkresult" >result.json
awkresult
that includes newlines and the output you want to see inresult.json
given that input. Right now we're all making different guesses about whether you mean a literal linefeed character or the 2-character string\n
in your input and your output. – Ed Morton May 02 '20 at 23:45$( ... )
notation is the recommended standard. Also, quoting is an important subject when it comes to shell variables. – AdminBee May 05 '20 at 14:33