Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide facility to escape double quotes #1918

Closed
ORESoftware opened this issue Jun 5, 2019 · 4 comments
Closed

Provide facility to escape double quotes #1918

ORESoftware opened this issue Jun 5, 2019 · 4 comments
Labels

Comments

@ORESoftware
Copy link

ORESoftware commented Jun 5, 2019

Here are a couple lines of a JSON in a file:

{"user":"alex","num":"509","time":"Tue Jun  4 22:26:08 PDT 2019","pwd":"/Users/alex/codes/ores/prompt-command","pid":8074,"exit_code":127,"cmd":"xxxx"}
{"user":"alex","num":"510","time":"Tue Jun  4 22:26:47 PDT 2019","pwd":"/Users/alex/codes/ores/prompt-command","pid":8114,"exit_code":0,"cmd":"sed '0,1d' "$HOME/my_bash_history""}
{"user":"alex","num":"511","time":"Tue Jun  4 22:26:55 PDT 2019","pwd":"/Users/alex/codes/ores/prompt-command","pid":8132,"exit_code":0,"cmd":"sed '0,1d' "$HOME/my_bash_history" | wc -l"}

not quote valid JSON because if you look at the bottom 2 lines, we have this pair:

"cmd":"sed '0,1d' "$HOME/my_bash_history" | wc -l"

so what we need is:

"cmd":"sed '0,1d' \"$HOME/my_bash_history\" | wc -l"

my question/request: does/can JQ provide a facility to escape/sanitize strings:

echo $cmd | jq --sanitize
@ORESoftware
Copy link
Author

for example, this is just unacceptably complicated:
https://unix.stackexchange.com/a/339892/287203

see my comment to the answer.

@pkoppstein
Copy link
Contributor

I believe the -R option does what you want. The following script:

#!/bin/bash

TEXT=$(cat<<"EOF"
sed '0,1d' "$HOME/my_bash_history" | wc -l
EOF
 )

echo "TEXT=$TEXT"

echo "$TEXT" | jq -R .

produces:

TEXT=sed '0,1d' "$HOME/my_bash_history" | wc -l
"sed '0,1d' \"$HOME/my_bash_history\" | wc -l"

@ORESoftware
Copy link
Author

thanks let me try it, I wonder if the . is necessary? probably not

@nicowilliams
Copy link
Contributor

So, I don't know how to implement a parser for "almost-right" JSON texts like the one you showed us, @ORESoftware...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants