JQ,如何根据条件计算?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (11)

使用jq,我需要根据两个标准获取数组:它必须有status === 'skipped' && ref.includes(version)

[
  {
    "id": 15484,
    "sha": "52606c8da57984d1243f436e5d12e275db29a6e0",
    "ref": "v1.4.15",
    "status": "canceled"
  },
  {
    "id": 15483,
    "sha": "52606c8da57984d1243f436e5d12e275db29a6e0",
    "ref": "v1.4.15",
    "status": "canceled"
  },
  {
    "id": 15482,
    "sha": "1b4ccc1dc17e9b8ddb24550c5566d2be6b03465e",
    "ref": "dev",
    "status": "success"
  },
  {
    "id": 15481,
    "sha": "5b6ec939739c5a1513634f3b58bf96522917571d",
    "ref": "dev",
    "status": "failed"
  },
  {
    "id": 15480,
    "sha": "ec18d46f491a4645c68388df91fc41455b421e71",
    "ref": "dev",
    "status": "failed"
  },
  {
    "id": 15479,
    "sha": "dd83a6d6e58cc5114aed8016341ab3c5b3ebb702",
    "ref": "dev",
    "status": "failed"
  },
  {
    "id": 15478,
    "sha": "18ccaf4bc37bf65470b2c6ddaa69e5b4018354a7",
    "ref": "dev",
    "status": "success"
  },
  {
    "id": 15477,
    "sha": "f90900d733bce2be3d9ba9db25f8b51296bc6f3f",
    "ref": "dev",
    "status": "failed"
  },
  {
    "id": 15476,
    "sha": "3cf0431a161e6c9ca90e8248af7b4ec39c54bfb1",
    "ref": "dev",
    "status": "failed"
  },
  {
    "id": 15285,
    "sha": "d24b46edc75d8f7308dbef37d7b27625ef70c845",
    "ref": "dev",
    "status": "success"
  },
  {
    "id": 15265,
    "sha": "52606c8da57984d1243f436e5d12e275db29a6e0",
    "ref": "v1.4.15",
    "status": "success"
  },
  {
    "id": 15264,
    "sha": "9a15f8d4c950047f88c642abda506110b9b0bbd7",
    "ref": "v1.4.15-static",
    "status": "skipped"
  },
  {
    "id": 15263,
    "sha": "9a15f8d4c950047f88c642abda506110b9b0bbd7",
    "ref": "v1.4.15-static",
    "status": "skipped"
  },
  {
    "id": 15262,
    "sha": "76451d2401001c4c51b9800d3cdf62e4cdcc86ba",
    "ref": "v1.4.15-no-js",
    "status": "skipped"
  },
  {
    "id": 15261,
    "sha": "76451d2401001c4c51b9800d3cdf62e4cdcc86ba",
    "ref": "v1.4.15-no-js",
    "status": "skipped"
  },
  {
    "id": 15260,
    "sha": "515cd1b00062e9cbce05420036f5ecc7a898a4bd",
    "ref": "v1.4.15-cli",
    "status": "skipped"
  },
  {
    "id": 15259,
    "sha": "515cd1b00062e9cbce05420036f5ecc7a898a4bd",
    "ref": "v1.4.15-cli",
    "status": "skipped"
  },
  {
    "id": 15258,
    "sha": "b67acd3082da795f022fafc304d267d3afd6b736",
    "ref": "v1.4.15-node",
    "status": "skipped"
  },
  {
    "id": 15257,
    "sha": "b67acd3082da795f022fafc304d267d3afd6b736",
    "ref": "v1.4.15-node",
    "status": "skipped"
  },
  {
    "id": 15256,
    "sha": "4da4a788a85d82527ea568fed4f03da193842a80",
    "ref": "v1.4.15-bs-redux-saga-router-dom-intl",
    "status": "skipped"
  }
]

我们还想使用环境变量进行查询:

  • status=skipped
  • ref=v1.4.15

这项工作,但没有环境变量选项:

cat test.json | jq '[.[] | select(.status=="skipped") | select(.ref | startswith("v1.4.15"))] | length'

这怎么可能?

提问于
用户回答回答于

length()将对象列表放入数组后,使用过滤器末尾的函数

jq '[.[] | select(.status == "skipped") | select(.ref | test("1\\.4\\.15"))] | length'

但是对于刚刚返回的对象省略了获取长度的逻辑

jq '[.[] | select(.status == "skipped") | select(.ref | test("1\\.4\\.15"))]'

test()是一种更强大的方法来匹配您的正则表达式与JSON字符串。的startswith()endswith()如果他们是在中间无法比拟的字符串。

使用变量,

ref="1\.4\.15"
jq --arg status "$status" --arg ref "$ref" \
    '[.[] | select(.status == $status) | select(.ref | test($ref))]|length' json
用户回答回答于

另外,一个简单的替代解决方案

bash $ status=skipped
bash $ ref=v1.4.15
bash $ cat test.json | jtc -w"[status]:<$status>: [-1] [ref]:<^$ref.*>R" | wc -l
       9
bash $ 

详细说明jtc步行路径(-w

  1. [status]:<$status>:- 首先,您找到符合skipped标签范围的所有条目status
  2. [-1] - 从所有找到的条目(在步骤1中)中升级1级(即找到找到的条目的父级)
  3. [ref]:<^$ref.*>R- 在找到的父条目中找到v1.4.15以标签开头的(使用RE)开头的第一个匹配项ref
    • 然后将所有找到的行传递给实用程序wc来计算行数

扫码关注云+社区

领取腾讯云代金券