JQ,如何根据条件进行计数?

时间:2019-02-11 06:10:11

标签: json linux bash debian jq

使用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'

这怎么可能?

答案:

status=skipped; ref=v1.4.15; cat test.json | jq --arg REF "$ref" --arg STATUS "$status" -r '[.[] | select(.status==$STATUS) | select(.ref | startswith($REF))] | length'

5 个答案:

答案 0 :(得分:4)

通过使用map(select(...)或同等功能,您可以使用function randomColor() { const r = Math.floor(Math.random() * 255); const g = Math.floor(Math.random() * 255); const b = Math.floor(Math.random() * 255); return 'rgb(' + r + ',' + g + ',' + b + ')'; } console.log(randomColor()),但是使用通用计数函数通常更有效,例如:

length

使用外壳和环境变量

jq手册中介绍了使用shell和环境变量的方法,但总而言之,一种传递字符串值的方法是使用命令行选项def sigma(s): reduce s as $s (null; .+$s); sigma(.[] | select(.status=="skipped" and (.ref | startswith("v1.4.15") )) | 1) ,例如遵循以下原则:

--arg

答案 1 :(得分:3)

在将对象列表放入数组之后,在过滤器的末尾使用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

答案 2 :(得分:1)

我知道jq在这里很受欢迎,但是我可以建议xidel吗?参见http://videlibri.sourceforge.net/xidel.html
就像jq一样,它是一个JSON解释器,但是除了JSONiq之外,您还可以使用XPath / Xquery函数来完成各种有趣的工作。

这将列出具有2个条件的所有对象:

xidel -s test.json -e '$json()[status="skipped" and starts-with(ref,"v1.4.15")]'

要对它们进行计数,只需使用count()函数将查询括起来:

xidel -s test.json -e 'count($json()[status="skipped" and starts-with(ref,"v1.4.15")])'

这将返回9

带有变量:

status=skipped
ref=v1.4.15

xidel -s test.json -e 'count($json()[status="'$status'" and starts-with(ref,"'$ref'")])'

答案 3 :(得分:0)

出于完整性考虑,这将是等效的JSONiq查询:

let $a := [
   (: copy-paste the entire array here in plain JSON syntax --
      omitted for the sake of brevity :)
]
return count(
   for $obj in $a[]
   where $obj.status eq "skipped"
         and
         matches($obj.ref, "ˆv")
   return $obj
)

答案 4 :(得分:-1)

还有一个简单的替代解决方案:

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为范围的ref(使用RE)开头的第一个匹配项

    • 然后将所有找到的行传递给实用程序wc以对行进行计数

PS。 jtc可以在github上找到:https://github.com/ldn-softdev/jtc