【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变量
相关推荐
永远睡不够的入1 小时前
C++list详解
c++·windows·list
feng_you_ying_li3 小时前
list的介绍与底层实现
数据结构·c++·list
勿芮介1 天前
【研发工具】OpenClaw基础环境安装全教程-Node\NVM\PNPM\Bash
开发语言·node.js·bash·ai编程
御坂10101号3 天前
「2>&1」是什么意思?半个世纪的 Unix 谜题
java·数据库·bash·unix
liu****3 天前
1.反向迭代器实现思路
数据结构·c++·反向迭代器·vector·list
co_wait3 天前
【C++ STL】list容器的基本使用
开发语言·c++·list
AI+程序员在路上3 天前
linux中bash与sh脚本区别
linux·运维·bash
路弥行至3 天前
linux运行脚本出现错误信息 /bin/bash^M: bad interpreter解决方法
linux·运维·开发语言·经验分享·笔记·其他·bash
记录无知岁月3 天前
【Linux】bash脚本使用
linux·bash
liulilittle3 天前
KList(基于 LinkedList 实现)
开发语言·数据结构·c++·list