从curl输出中搜索并打印字符串和值

时间:2014-06-04 15:03:43

标签: php bash unix curl

我有一个curl命令,它返回一行,但有一个冗长的响应体。并且需要搜索特定字符串并打印结果。

例如,这是curl的输出。我可以使用bash脚本打印centos的状态值,这是" true"?或者我应该使用不同的脚本/程序语言?脚本应该基本输出为state = true。注意,有两个状态"输出中的值

~]# 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}]

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此sed组合命令

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

示例:

$ 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