【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变量
相关推荐
AI殉道师7 小时前
从0开发大模型之实现Agent(Bash到SKILL)
开发语言·bash
迪霸戈8 小时前
MyBatis动态SQL避坑:为什么List用`[0]`而不是`get(0)`
sql·list·mybatis
张火火isgudi9 小时前
fedora 下使用 oh-my-posh 美化 bash
linux·bash
星谐9 小时前
Bash 双模式解析 + 飞书机器人:圈复杂度报告 45 min→30 s 实战
servlet·bash·飞书
番茄灭世神20 小时前
常见终端工具输出中文乱码的解决方案
bash·编码格式·powershell·终端工具
txinyu的博客1 天前
list 三个经典版本
数据结构·list
一叶之秋14121 天前
深入剖析List的底层实现原理
c++·list
万象.2 天前
redis数据结构list的基本指令
数据结构·redis·list
caz282 天前
git bash突然ssh不能用了
git·ssh·bash
初願致夕霞2 天前
实现具备C++11现代特性的STL——list篇(使用shared_ptr智能指针实现,解决了循环引用问题)
c++·list