命令行中的正则表达式匹配多行字符串

3

我正在尝试使用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对象。

2个回答

4
这个 sed 命令将会处理好如果我正确理解了 JSON 部分的话。
从第一行以 { 开头一直打印到文件结尾:
sed -n '/^{/,$ p' test.txt

0
Perl有一个很棒的命令行开关,可以将您置于“段落”模式而不是逐行阅读。然后,您只需要跳过第一个段落即可:
perl -00 -ne 'print unless $. == 1' test.txt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接