0

No jq, no grep -Po, even no awk. Only bash.

Goal: simulate grep -Po '(?<="code":")\w+(?=")' on bash.

It's extraction of code value from json.

Json example:

{
  "content": [
    {
      "createdBy":"admin",
      "createdDate":"2019-04-01T07:07:37+0000",
      "lastModifiedBy":"admin",
      "lastModifiedDate":"2019-04-01T07:07:37+0000",
      "code":"ABC123"
    }
  ],
  "first": true,
  "last": true,
  "totalPages": 1,
  "totalElements": 1,
  "numberOfElements": 1,
  "size": 10,
  "number": 0,
  "sort": null
}

Expecting result: ABC123.


My attempt:

CODE='{
  "content": [
    {
      "createdBy":"admin",
      "createdDate":"2019-04-01T07:07:37+0000",
      "lastModifiedBy":"admin",
      "lastModifiedDate":"2019-04-01T07:07:37+0000",
      "code":"ABC123"
    }
  ],
  "first": true,
  "last": true,
  "totalPages": 1,
  "totalElements": 1,
  "numberOfElements": 1,
  "size": 10,
  "number": 0,
  "sort": null
}'
echo "${CODE//[code":"\w+]/}"

It replaces, but I want substring... It was a try to use smth like that https://stackoverflow.com/a/1916293/4386227

Woland
  • 623
  • 2
  • 13
  • 31

0 Answers0