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

Linux环境使用jq对JSON数据集实现两类指定条件查询

jq查询实现方案

1. 筛选screensProcesses数组长度小于2的单元

直接遍历units下的所有单元,按数组长度筛选即可,命令如下:

# 输出符合条件的单元ID+完整信息
jq '.units | to_entries[] | select(.value.screensProcesses | length < 2) | {unit_id: .key, detail: .value}' 你的json文件名.json

# 仅输出符合条件的单元ID
jq -r '.units | to_entries[] | select(.value.screensProcesses | length < 2) | .key' 你的json文件名.json

2. 筛选超过24小时未签到的单元

大体积数据集优先方案

先调整你JSON中的3_lastCheckIn写入格式为秒级Unix时间戳,无需额外跨进程调用,查询效率最高,命令如下:

jq '.units | to_entries[] | select((now - .value."3_lastCheckIn") > 86400) | {unit_id: .key, hostname: .value."1_hostname", last_checkin_ts: .value."3_lastCheckIn"}' 你的json文件名.json

其中86400是24小时对应的秒数,你可以按需调整。

兼容原有日期格式方案(仅适合小文件)

如果不想修改现有日期格式,可以配合系统date命令做时间转换,命令如下:

jq -r '.units | to_entries[] | "\(.key) \(.value."3_lastCheckIn") \(.value."1_hostname")"' 你的json文件名.json | while read id checkin host; do
  checkin_ts=$(date -d "$checkin" +%s)
  now_ts=$(date +%s)
  if [ $((now_ts - checkin_ts)) -gt 86400 ]; then
    echo "单元ID: $id 主机名: $host 上次签到时间: $checkin"
  fi
done

该方案需要逐行调用date命令,大体积数据集执行效率较低,不推荐使用。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 22:39:04