1

I am writing a KSH shell script. It reads from a JSON file, which contains control characters like \n, \r and \t in place of actual newline characters or tabs (so the control characters are being read as actual strings). I am using jq to parse the stuff I want from this JSON file, and appending it to a text file. This text file also ends up having the strings like \n, \t instead of newline, tabs, etc. I want the text file to have actual meaning of these control characters.

For example, for the string "I\nam\na\ngood\nboy", I want my file to have:

I
am
a
good
boy

Any ideas on how to accomplish this in a shell script?

Note: I am writing in KSH, but answers in bash etc. would be acceptable.

lebowski
  • 1,031
  • 2
  • 20
  • 37
  • How to change `"I\nam\na\ngood\nboy"` to the relevant five-line string is trivial. *Making sure the result is valid markdown* is what's potentially hard. If markdown isn't really part of the question, you might want to remove it? – Charles Duffy Dec 01 '17 at 01:36
  • Hmm. If not an exact duplicate, this is very near to https://stackoverflow.com/questions/44656515/how-to-remove-double-quotes-in-jq-output-for-parsing-json-files-in-bash/44656583#44656583 – Charles Duffy Dec 01 '17 at 01:50

1 Answers1

5

Use the -r argument to tell jq to use raw strings for its output.

jq -r . <<EOF
"I\nam\na\ngood\nboy"
EOF

...emits the precise output:

I
am
a
good
boy
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441