【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变量
相关推荐
Pasregret4 小时前
策略模式:动态切换算法的设计智慧
算法·bash·策略模式
hy.z_77720 小时前
【数据结构】线性表( List)和 顺序表(ArrayList)
数据结构·list
xrkhy1 天前
Collection集合,List集合,set集合,Map集合
java·数据结构·list
Kay_Liang1 天前
Java集合框架中的List、Map、Set详解
java·开发语言·数据结构·蓝桥杯·list
spencer_tseng2 天前
List findIntersection & getUnion
java·list
weixin_456588152 天前
【java 13天进阶Day05】数据结构,List,Set ,TreeSet集合,Collections工具类
java·数据结构·list
Lary_c2 天前
【Redis】字符串类型List 常用命令详解
redis·缓存·list
维维宝宝最可爱啦QWQ2 天前
深入解析C++ STL List:双向链表的特性与高级操作
c++·链表·list
大炮筒3 天前
CPPlist初识
数据结构·c++·list
SpikeKing3 天前
Server - 优雅的配置服务器 Bash 环境(.bashrc)
服务器·bash·bashrc·bash_profile