hookedonwinter
hookedonwinter

Reputation: 12666

regex match against multiple lines at the command line

I'm playing around with a bash script, parsing the output of a cURL POST, etc. Not my forte, but a fun project.

The result of my cURL contains the header information as well as a big json object. I only want the json. Here is the output. (I'm creating a new gist on 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": "[email protected]: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
}

I only want the json part of this, and was attempting to do so with sed. The above content is stored in a file called test.txt.

$ cat test.txt | sed 's/.*\({.*}\)/\1/'

This isn't working. So, my question is how to make that last command only show the JSON object.

Upvotes: 3

Views: 350

Answers (2)

glenn jackman
glenn jackman

Reputation: 246807

Perl has a neat command line switch that puts you in "paragraph" mode instead of reading line-by-line. Then, you just need to skip the 1st paragraph:

perl -00 -ne 'print unless $. == 1' test.txt

Upvotes: 0

Birei
Birei

Reputation: 36262

This sed command will do the job if I understand right what is the JSON part.

Print from first line beginning with { until end of file:

sed -n '/^{/,$ p' test.txt

Upvotes: 4

Related Questions