문제

I'm using jq to read some data from a JSON file.

after=`cat somefile.json | jq '.after[]'`

returns something like this:

"some value" "another value" "something else"

Basically a list of quoted strings. I now need to convert these strings into one string formatted like

"some value; another value; something else;"

I've tried a lot of combinations of for loops to try and get this working and nothing quite works.

Anyone know how this can be done? Cheers!

도움이 되었습니까?

해결책 2

Thanks all! I actually decided to dig deeper into the jq docs to see if I could simply leverage it to do what I want.

after=`cat somefile.json | jq -c -r '.after[] + "; "'` | tr -d '\n'

This ended up working very well. Thanks for the sed version though! Always good to see another working solution.

다른 팁

use sed:

sed -e 's/" /; /g; s/ "/ /g; s/"$/;"/' <<< '"some value" "another value" "something else"'

OUTPUT:

"some value; another value; something else;"

use sed s command for replacing the desire value

Assuming .after[] returns the list of strings you describe, you can do this entirely with jq using join to format them as follows:

[ .after[] ] | join("; ") + ";"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top