当前位置: 首页 > news >正文

linux jq命令详解

jq 是一个强大的命令行工具,用于处理 JSON 格式的数据。它可以帮助你查询、过滤、修改和处理 JSON 数据,使得在命令行环境下处理 JSON 变得非常方便。

安装

yum install  -y jq

命令

jq [options] <jq filter> [file...]
jq [options] --args <jq filter> [strings...]
jq [options] --jsonargs <jq filter> [JSON_TEXTS...]

options

Some of the options include:--compact-output, -c               # 以压缩格式输出--null-input, -n               use `null` as the single input value;-e               # 基于输出设置 exit 状态(set the exit status code based on the output);--slurp , -s               read (slurp) all inputs into an array; apply filter to it;--raw-output, -r               #输出原始字符串,不是json格式。(output raw strings, not JSON texts);--join-output , -j--raw-input, -R               #读原始字符串,而不是json文本( read raw strings, not JSON texts);-C               #使用彩色。colorize JSON;-M               #不使用彩色。 monochrome (don't colorize JSON);--sort-keys , -S               #输出key排序。(sort keys of objects on output);--indent n       # 指定n个空格作为缩进--tab            # use tabs for indentation;--arg a v        # set variable $a to value <v>;-f filename / --from-file filename  #从文件读取filter--argjson a v    # set variable $a to JSON value <v>;--slurpfile a f  # set variable $a to an array of JSON texts read from <f>;--rawfile a f    # set variable $a to a string consisting of the contents of <f>;--args           # 把参数解析为字符串,而不是文件名。 remaining arguments are string arguments, not files;--jsonargs       # 把参数解析为json文本,而不是文件名。 remaining arguments are JSON arguments, not files;--               #参数结束 terminates argument processing;

filter

  • .: 表示当前对象,用于访问字段或属性。
  • ,:多个filter
  • |:管道
  • .fieldName: 选择指定字段的值。
  • [fieldName]: 选择指定字段的值。
  • [fieldName]?: 选择指定字段的值,属性不存在则不报错,返回null
  • .fieldName?: 选择指定字段的值,属性不存在则不报错,返回null
  • []: 用于遍历数组元素,返回iterator。
  • [<number>]: 用于选择元素。
  • .[<number>:<number>]:切片
  • select(condition): 根据条件选择元素。
  • map(transform): 对数组中的每个元素应用转换操作。

示例

测试数据

{"code": 0,"msg": null,"data": [{"name": "采集服务","entry": "/jcollect/","instances": [{"ip": "192.168.1.X",                                   "port": 8888},{"ip": "192.168.1.X",                                   "port": 8889}]},{"name": "模型服务","entry": "/jmodel/","instances": [{"ip": "192.168.1.X",                                   "port": 8899},{"ip": "192.168.1.X",                                   "port": 8898}]}]
}

输出参数

▶ json格式化:

 cat data.json | jq ' .data[0]'#json格式化
{"name": "采集服务","entry": "/jcollect/","instances": [{"ip": "192.168.1.X","port": 8888},{"ip": "192.168.1.X","port": 8889}]
}

▶ 压缩输出:

 cat data.json | jq -c ' .data[0]'
{"name":"采集服务","entry":"/jcollect/","instances":[{"ip":"192.168.1.X","port":8888},{"ip":"192.168.1.70","port":8889}]}

▶ -r:

 cat data.json | jq -r ' .data[0]'
{"name": "采集服务","entry": "/jcollect/","instances": [{"ip": "192.168.1.X","port": 8888},{"ip": "192.168.1.X","port": 8889}]
}

▶ tab缩进:

 cat data.json | jq -r --tab ' .data[0]'
{"name": "采集服务","entry": "/jcollect/","instances": [{"ip": "192.168.1.X","port": 8888},{"ip": "192.168.1.X","port": 8889}]
}

基础Filter

  • .[].fieldName
 cat data.json | jq   ' .data[].name'
"采集服务"
"模型服务"
  • [number]
 cat data.json | jq   ' .data[].name'
"采集服务"
"模型服务"

构造器

  • 数组构造器:[]
  • 对象构造器: {}
  • ..:路径打平,类似XPath//

内置operator和function

  • +
  • -
  • *
  • /
  • %
  • abs
  • length
  • utf8bytelength
  • keys, keys_unsorted
  • has(key)
  • in
  • map(f), map_values(f)
  • pick(pathexps)
  • path(path_expression)
  • del(path_expression)
  • getpath(PATHS)
  • setpath(PATHS; VALUE)
  • delpaths(PATHS)

条件和比较

  • ==, !=
  • if-then-else-end
  • >, >=, <=, <
  • and, or, not
  • //:Alternative operator

正则表达式

  • test(val)
  • test(regex; flags)
  • match(val)
  • match(regex; flags)

附录

参考

GitHub 地址:https://github.com/stedolan/jq

jq 官方网站: https://stedolan.github.io/jq/

文档:https://jqlang.github.io/jq/tutorial/

手册:https://jqlang.github.io/jq/manual/#invoking-jq

在线工具:https://jqplay.org/

http://www.lqws.cn/news/479557.html

相关文章:

  • 基于深度学习的智能图像风格迁移系统:技术与实践
  • Spring AI 项目实战(十一):Spring Boot +AI + DeepSeek 开发智能教育作业批改系统(附完整源码)
  • 华为云Flexus+DeepSeek征文|华为云 Dify 高可用部署教程:CCE 容器集群一键构建企业级智能应用
  • 【第一章-计算机系统概述】
  • 鸿蒙ArkTs仿网易云音乐项目:架构剖析与功能展示
  • 对射式红外传感器计次旋转编码器计次
  • 第八章 网络安全
  • 减少推实时视频流的延时,要提高摄像头的帧率吗
  • openCV
  • openai-agents实现input_guardrails
  • 策略设计模式
  • 使用 RedisVL 进行复杂查询
  • Vue 组件定义方式的区别
  • Rabbitmq集成springboot 使用死信队列
  • day 39 打卡
  • 10-K 和 10-Q是什么?
  • MySQL基础函数篇
  • DubboSPI
  • 如何在FastAPI中玩转GitHub认证,让用户一键登录?
  • 安卓对外发布工程源码:怎么做到仅UI层公布
  • Openwrt基本初始化(安装中文包,磁盘扩容)
  • MATLAB的.mat文件
  • Python 商务数据分析—— NumPy 学习笔记Ⅱ
  • Spark教程1:Spark基础介绍
  • 爬虫入门练习(文字数据的爬取)
  • Vue3解析Spring Boot ResponseEntity
  • “MOOOA多目标鱼鹰算法在无人机多目标路径规划
  • 2025国际无人机应用及防控大会四大技术专题深度解析
  • 算法-动态规划-钢条切割问题
  • 理解后端开发中的中间件(以gin框架为例)