On OS X, you can use the Perl system:
curl -sS http://xkcd.com/info.0.json | /usr/bin/perl -pe 's/.*"num": ([0-9]+).*/\1/'
You can save the output in a variable with the replacement of the command:
num=$(curl -sS http://xkcd.com/info.0.json | /usr/bin/perl -pe 's/.*"num": ([0-9]+).*/\1/') curl -sS "http://xkcd.com/${num}/info.0.json"
or more briefly, two in one, although not very readable:
curl -sS "http://xkcd.com/$(curl -sS http://xkcd.com/info.0.json | /usr/bin/perl -pe 's/.*"num": ([0-9]+).*/\1/')/info.0.json"
By the way, I highly recommend jq as a JSON command line processor. To extract num with jq is as simple as
curl -sS http://xkcd.com/info.0.json | jq '.num'
and although you didn’t ask for it, here is a simple single line with jq that retrieves the URI of the last image:
curl -sS "http://xkcd.com/$(curl -sS http://xkcd.com/info.0.json | jq '.num')/info.0.json" | jq -r '.img'
Output Example:
http:
source share