我正在玩一个bash脚本,解析cURL POST的输出等等。不是我的强项,而是一个有趣的项目。
我的cURL的结果包含头信息以及一个大的json对象。我只想要json。这是输出。 (我在github上创建了一个新的要点)
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对象。
答案 0 :(得分:3)
如果我理解sed
部分是什么,这个JSON
命令就可以完成工作。
从以{
开头的第一行打印到文件结尾:
sed -n '/^{/,$ p' test.txt
答案 1 :(得分:0)
Perl有一个简洁的命令行开关,可以让你进入“段落”模式而不是逐行阅读。然后,您只需要跳过第1段:
perl -00 -ne 'print unless $. == 1' test.txt