【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变量
相关推荐
IMPYLH14 小时前
Linux 的 split 命令
linux·运维·python·bash·运维开发·unix
IMPYLH17 小时前
Linux 的 stat 命令
linux·运维·服务器·bash
SariHcr12318 小时前
Openarm机器人双臂模型仿真从零部署
c++·人工智能·python·机器人·bash·openarm
IMPYLH2 天前
Linux 的 sleep 命令
linux·运维·服务器·bash
沉默金鱼2 天前
读书笔记——U3D高级编程:主程手记——第二章2.2List底层源码
数据结构·list
IMPYLH3 天前
Linux 的 shuf 命令
linux·运维·服务器·bash
IMPYLH3 天前
Linux 的 shred 命令
linux·运维·服务器·bash
IT摆渡者3 天前
Linux 巡检脚本BASH
linux·运维·bash
NoSi EFUL3 天前
redis存取list集合
windows·redis·list