【bash工具】jq遍历list

jq遍历list的两种方式

方法一:read -r + while循环 按行读取

bash 复制代码
$rows=$(cat file.json | jq -rc '@base64')
while IFS= read -r line; do    # IFS必须为空,才能正确按行处理
	echo $line | jq -r '.ID'   # 获取ID字段
done < file    # 读取文件内容
# done <<< "$str"  # here string方式读取bash变量

方法二:base64编码 + for循环 读取

bash 复制代码
$rows=$(cat file.json | jq -rc '@base64') # 紧凑方式输出json并编码,这样就没有空格字符
# for只能按空白字符(空格、换行符)来分割string。所以必须先用base64编码去除空格
for row in $rows; do
	echo $row | base -d | jq -r '.ID'   # base64解码,获取ID字段
done < file    # 读取文件内容
# done <<< "$str"  # here string方式读取bash变量
相关推荐
Engineer-Jsp9 小时前
A problem occurred starting process ‘command ‘bash‘‘
开发语言·bash
HABuo11 小时前
【linux进程控制(三)】进程程序替换&自己实现一个bash解释器
linux·服务器·c语言·c++·ubuntu·centos·bash
小学生波波11 小时前
HarmonyOS6 - 鸿蒙双向滚动课程表案例
list·scroll·鸿蒙开发·课程表·harmonyos6
AI殉道师1 天前
从0开发大模型之实现Agent(Bash到SKILL)
开发语言·bash
迪霸戈1 天前
MyBatis动态SQL避坑:为什么List用`[0]`而不是`get(0)`
sql·list·mybatis
张火火isgudi2 天前
fedora 下使用 oh-my-posh 美化 bash
linux·bash
星谐2 天前
Bash 双模式解析 + 飞书机器人:圈复杂度报告 45 min→30 s 实战
servlet·bash·飞书
番茄灭世神2 天前
常见终端工具输出中文乱码的解决方案
bash·编码格式·powershell·终端工具
txinyu的博客2 天前
list 三个经典版本
数据结构·list
一叶之秋14122 天前
深入剖析List的底层实现原理
c++·list