You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用通配符在jq中按起始名称筛选满足延迟条件的数值

筛选符合条件的JSON数据的jq命令修改方案

需求回顾

需要从给定的JSON数据中筛选出满足以下两个条件的对象:

  • source字段以app1.或app2.开头
  • latency字段数值大于10

修改后的jq命令

你可以通过两种方式修改原有命令,实现同时匹配app1和app2开头的来源:

方法1:使用or连接多个startswith判断

jq -r '.[] | .end2end.data[0] | select(.latency > 10) | select(.source | startswith("app1") or startswith("app2"))' all_info.json

核心修改点是将原命令中单一的startswith("app1")替换为startswith("app1") or startswith("app2"),通过逻辑或运算符同时匹配两类前缀的来源。

方法2:使用正则表达式匹配(更简洁)

jq -r '.[] | .end2end.data[0] | select(.latency > 10) | select(.source | test("^app[12]\\."))' all_info.json

这里用正则表达式^app[12]\\.实现匹配:

  • ^表示字符串开头
  • app[12]匹配app1或app2
  • \\.转义点号(避免正则中点号的通配符特性)

执行结果

运行上述命令后,会输出以下两条符合条件的数据:

{
  "at": 16054343445,
  "source": "app1.btch1_src",
  "latency": 200.5,
  "target": "app1.btch1_tgt"
}
{
  "at": 16054341234,
  "source": "app2.btch_src",
  "latency": 50.4,
  "target": "app2.btch_tgt"
}

内容的提问来源于stack exchange,提问作者swathi k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 11:03:25