0

I have strings in the following pattern: <SOMETHING_1>{<JSON>}<SOMETHING_2>

I want to keep the <JSON> and remove the <SOMETHING_X>blocks. I'm trying to do it with substring removal, but instead of getting

{x:1,action:PLAYING,name:John,description:Some description rSxv9HiATMuQ4wgoV2CGxw}

I keep getting

{x:1,action:PLAYING,name:John,description:Some}

because the whitespace in the description field cuts off the substring.

Any ideas on what to change?

CODE:

string="000{x:1,action:PLAYING,name:John,description:Some description rSxv9HiATMuQ4wgoV2CGxw}401"
string=$1
string=$(echo "${string#*{}")
string=$(echo "${string%}*}")
string={$string}
echo $string
5
  • Does your JSON have any hashes {...} inside? Commented Jun 2, 2017 at 22:40
  • I'm not getting the output you showed. How do you pass the argument to the script? Commented Jun 2, 2017 at 22:42
  • Regardless of correctness, the performance overhead of subshells for the command substitutions is a bit unfortunate. Commented Jun 2, 2017 at 22:43
  • 1
    BTW, I suspect rather strongly that the value is actually getting cut off somewhere else -- for instance, if you're passing it into $1 by invoking yourscript $somevar that would cause the problem, on account of needing to be yourscript "$somevar"; everything after the space is going into $2 or a subsequent argument. Commented Jun 3, 2017 at 3:15
  • What's the string=$1 line for? Commented Jun 4, 2017 at 9:31

3 Answers 3

1

The original code works perfectly, if we accept a direct assignment of the string -- though the following is a bit more explicit:

string="000{x:1,action:PLAYING,name:John,description:Some description rSxv9HiATMuQ4wgoV2CGxw}401"
string='{'"${string#*"{"}"  # trim content up to and including the first {, and replace it
string="${string%'}'*}"'}'  # trim the last } and all after, and replace it again
printf '%s\n' "$string"

...properly emits:

{x:1,action:PLAYING,name:John,description:Some description rSxv9HiATMuQ4wgoV2CGxw}

I'm guessing that the string is being passed on a command line unquoted, and is thus being split into multiple arguments. If you quote your command-line arguments to prevent string-splitting by the calling shell (./yourscript "$string" instead of ./yourscript $string), this issue will be avoided.

Sign up to request clarification or add additional context in comments.

1 Comment

You were right in your comments above. It was the $1 that was causing it to get cut off. Thanks a ton! Please add that to your answer so I can accept it.
0

with sed:

string="000{x:1,action:PLAYING,name:John,description:Some description rSxv9HiATMuQ4wgoV2CGxw}401"
sed 's/.*\({.*}\).*/\1/g' <<<$string

output:

{x:1,action:PLAYING,name:John,description:Some description rSxv9HiATMuQ4wgoV2CGxw}

Comments

0

Here you go…

string="000{x:1,action:PLAYING,name:John,description:Some description rSxv9HiATMuQ4wgoV2CGxw}401"
echo "original: ${string}"
string="${string#*\{}"
string="${string%\}*}"
echo "final: {${string}}"

By the way, JSON keys should be surrounded with double quotes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.