I'm working on a short bash script to grab a JSON element from a curl response.
curl -H "api_key:[API_PASSWORD]" http://api.wordnik.com/v4/word.json/button/pronunciations?sourceDictionary=macmillan&typeFormat=IPA&useCanonical=false
returns:
[{"id":0,"seq":0,"raw":"ˈbʌt(ə)n","rawType":"IPA"},{"id":0,"seq":0,"raw":"ˈbʌt(ə)n","rawType":"IPA"}]
I'm trying to extract the "ˈbʌt(ə)n" element.
Though I'm unfamiliar with regex, I think I should be using a substitution with this string:
/.*"(.*)",/
I'm trying to run the following command, but it doesn't seem to work:
curl -H "api_key:[API_KEY]" http://api.wordnik.com/v4/word.json/button/pronunciations?sourceDictionary=macmillan&typeFormat=IPA&useCanonical=false | sed /.*"(.*)",\1/
I'm sure there are a few things I'm doing wrong, and after a few hours of searching and reading up on regex and bash I'm out of options.
I don't need to be using sed, I am simply looking for a quick way of doing this in a bash command line so that I can implement it in a TextExpander script on the mac.
Use STRING : REGEXP
to extract the value from the json string:
string=$(curl -H "api_key:[API_PASSWORD]" http://api.wordnik.com/v4/word.json/button/pronunciations?sourceDictionary=macmillan&typeFormat=IPA&useCanonical=false)
raw=$(expr "$string" : '.*"raw":"\([^"]*\)"')
echo $raw
See man expr
:
STRING : REGEXP
anchored pattern match of REGEXP in STRING
Pattern matches return the string matched between \( and \) or null
Regular expressions may not be the right thing to use. http://www.codinghorror.com/blog/2008/06/regular-expressions-now-you-have-two-problems.html
On Ubuntu 9.10:
$ sudo apt-get install jsonlib-perl
$ curl -quiet 'http://api.wordnik.com/v4/word.json/button/pronunciations?sourceDictionary=macmillan&typeFormat=IPA&useCanonical=false' | perl -e 'use JSON; print JSON->new->allow_nonref->decode(<>)->{raw}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With