0

I have a curl command which returns a single line but having lengthy response body. And need to search for particular string and print the result.

For example, here is the output of curl. Can I use a bash script to print the state value of centos which is "true"? or should I use a different script/program language? The script should basically output as state=true. Note, there are two "state" values in the output

~]# curl -v -k -u test:test https://myurl.com/api/state



"name":"centos","type":"disc","state":true},{"partitions":5},"auth_mechanisms":  [{"name":"PLAIN","description":"PLAIN authentication"}],   [{"name":"suse","type":"ram","state":true}]
user3331975
  • 2,647
  • 7
  • 28
  • 30
  • It looks like a JSON to me. A JSON parser like [jq](http://stedolan.github.io/jq/) may be needed perhaps? – konsolebox Jun 04 '14 at 15:07
  • Is the output just json? If so you may want to use python's (really awesome) [library](http://stackoverflow.com/questions/4759634/python-json-tutorial). That way you can just use all the dictionary operations (I don't use python much day-to-day so I think I can comfortably say the learning curve isn't too bad; some of the first things I wrote in python used this). – Parthian Shot Jun 04 '14 at 15:08
  • It's worth noting that [json is a subset of yaml](http://stackoverflow.com/a/1729545/3680301), so if you have a yaml parser that'll also work. – Parthian Shot Jun 04 '14 at 15:13
  • yes, it is only json. – user3331975 Jun 04 '14 at 15:16
  • Thanks for the response. Looks like python script will be more efficient to accomplish this task. However I am new to Python, but will see how close I can get this script done. Thanks again! – user3331975 Jun 04 '14 at 16:06

1 Answers1

0

You could try this combined sed command,

sed 's/},/\n/g' file | sed -nr '/centos/ s/.*:(.*)$/\1/p'

Example:

$ echo '"name":"centos","type":"disc","state":true},{"partitions":5},"auth_mechanisms":  [{"name":"PLAIN","description":"PLAIN authentication"}],   [{"name":"suse","type":"ram","state":true}]' | sed 's/},/\n/g' | sed -nr '/centos/ s/.*:(.*)$/\1/p'
true
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Thanks much for the help.. Could you please let me know how to extract the name and it's respective state. For example, echo centos==true, suse==true. Basically, my intention is to grep the state against each name, and print the result – user3331975 Jun 04 '14 at 16:13