【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变量
相关推荐
IMPYLH1 天前
Linux 的 rm 命令
linux·运维·服务器·网络·bash
想唱rap2 天前
C++智能指针
linux·jvm·数据结构·c++·mysql·ubuntu·bash
噢,我明白了2 天前
Java 入门,详解List,Map集合使用
java·list·map
小马_xiaoen2 天前
前端虚拟列表(Virtual List)从原理到实战:海量数据渲染终极方案
前端·数据结构·list
IMPYLH2 天前
Linux 的 pwd 命令
linux·运维·服务器·bash
IMPYLH2 天前
Linux 的 readlink 命令
linux·运维·服务器·网络·bash
Rsun045513 天前
13、Java 策略模式从入门到实战
java·bash·策略模式
IMPYLH3 天前
Linux 的 printf 命令
linux·运维·服务器·bash
Jul1en_3 天前
【Redis】List列表命令、编码方式及应用场景
数据库·redis·list
IMPYLH4 天前
Linux 的 printenv 命令
linux·运维·服务器·bash