You can use jq
, in your example it would be something like this:
#! /bin/bash
content="{\"percent\":\"13.29471501108489\",\"count\":1859}"
echo $content
This has only been to generate the same response that generates the curl
, now we extract the value:
percentValue=$(echo $content | jq ".[\"percent\"]")
echo $percentValue
With this you will get the result you want:
"13.29471501108489"
If you wanted the value without quotes you can add the sentence 'tr:
percentValue=$(echo $content | jq .[\"percent\"] | tr -d '"')
Here is the complete example:
#! /bin/bash
content="{\"percent\":\"13.29471501108489\",\"count\":1859}"
echo $content
percentValue=$(echo $content | jq ".[\"percent\"]")
echo $percentValue
Now a complete example with curl
:
#!/bin/bash
content=$(curl -L url)
echo $content
percentValue=$(echo $content | jq ".[\"percent\"]")
echo $percentValue
To install jq
you can guide here , but if you're on OSX it's as easy as:
brew install jq
In Ubuntu it would be like this:
sudo apt-get install jq