【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变量
相关推荐
Severus_black7 小时前
C实现双向链表和相关函数!巨详细!
c语言·数据结构·链表·list
IMPYLH8 小时前
Linux 的 nl 命令
linux·运维·服务器·bash
Magic--9 小时前
C++ STL中vector与list的核心区别
c++·windows·list
li1670902701 天前
第十章:list
c语言·开发语言·数据结构·c++·算法·list·visual studio
游乐码1 天前
C#List
开发语言·c#·list
坏小虎1 天前
~/.zshrc 和 ~/.bash_profile 详细介绍与区别
开发语言·bash
想唱rap1 天前
计算机网络基础
linux·计算机网络·mysql·ubuntu·bash
代码改善世界1 天前
【C++初阶】双向循环链表:List底层结构的完整实现剖析
c++·链表·list
要记得喝水1 天前
适用于 Git Bash 的脚本,批量提交和推送多个仓库的修改
git·elasticsearch·bash
IMPYLH1 天前
Linux 的 mv 命令
linux·运维·服务器·bash