我正在尝试使用bash脚本解析cURL POST的输出等内容,虽然这不是我的强项,但这是一个有趣的项目。
cURL的结果包含头信息和一个大的json对象。我只想要json。以下是输出内容。(我在github上创建了一个新的gist)
HTTP/1.1 201 Created
Server: nginx/1.0.12
Date: Wed, 07 Mar 2012 22:19:59 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 201 Created
X-RateLimit-Limit: 5000
ETag: "8f778806263bd5c7b35a4d03f98663f7"
Location: https://api.github.com/gists/1996642
X-RateLimit-Remaining: 4989
Content-Length: 1042
{
"html_url": "https://gist.github.com/1996642",
"files": {
"test.diff": {
"content": "this is content",
"type": "text/plain",
"raw_url": "https://gist.github.com/raw/1996642/434713954dc8d57f923dec99d82610828c5ef714/test.diff",
"language": "Diff",
"size": 15,
"filename": "test.diff"
}
},
"git_pull_url": "git://gist.github.com/1996642.git",
"forks": [
],
"history": [
{
"change_status": {
"additions": 1,
"deletions": 0,
"total": 1
},
"user": null,
"url": "https://api.github.com/gists/1996642/2659edea4f102149b939558040ced8281ba8a505",
"version": "2659edea4f102149b939558040ced8281ba8a505",
"committed_at": "2012-03-07T22:19:59Z"
}
],
"public": true,
"git_push_url": "git@gist.github.com:1996642.git",
"comments": 0,
"updated_at": "2012-03-07T22:19:59Z",
"user": null,
"url": "https://api.github.com/gists/1996642",
"created_at": "2012-03-07T22:19:59Z",
"id": "1996642",
"description": null
}
我只需要其中的json部分,尝试使用sed来提取。上述内容存储在名为test.txt
的文件中。
$ cat test.txt | sed 's/.*\({.*}\)/\1/'
这个不起作用。我的问题是如何使最后一个命令只显示JSON对象。