jq: How to output quotes on raw output on windows

非 Y 不嫁゛ 提交于 2021-02-16 18:02:33

问题


Using raw output I have to quote some values of the output.

echo [{"a" : "b"}] | jq-win64.exe --raw-output ".[] | \"Result is: \" + .a + \".\""

generates

Result is: b.

but how can I generate

Result is: "b".

Unfortunately it has to run on Windows called from inside a CMD file.


回答1:


You need to escape the slashes to escape a "

$ echo [{"a" : "b"}] | jq-win64.exe --raw-output ".[] | \"Result is: \\\"\" + .a + \"\\\".\""
Result is: "b".



回答2:


A hacky workaround with less backslashing could be:

jq -r ".[] | \"Result is: \" + (.a|tojson)"

[REVISED to reflect OP goal.]




回答3:


Since you're trying to output double quotes in a double quoted string, you need to escape the inner quotes. And to escape the inner quotes, you need to also escape the escaping backslashes. So a literal double quote would have to be entered as \\\". You can do this a little cleaner by using string interpolation instead of regular string concatenation.

jq -r ".[] | \"Result is: \\\"\(.a)\\\".\""


来源:https://stackoverflow.com/questions/35204397/jq-how-to-output-quotes-on-raw-output-on-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!