如何仅使用纯bash提取json值?

时间:2019-06-13 07:54:49

标签: regex bash grep

没有jq,没有grep -Po,甚至没有awkbash

目标:在bash上模拟grep -Po '(?<="code":")\w+(?=")'

这是从json中提取代码值。

Json示例:

{
  "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
}

预期结果:ABC123


我的尝试

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+]/}"

它替换了,但是我想要子字符串... 尝试使用类似https://stackoverflow.com/a/1916293/4386227

的smth

0 个答案:

没有答案