【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 小时前
STL?list!!!
c语言·开发语言·数据结构·c++·windows·list
寒小松13 小时前
Problem E: List练习
java·数据结构·list
我来整一篇2 天前
用Redis的List实现消息队列
数据库·redis·list
周Echo周2 天前
20、map和set、unordered_map、un_ordered_set的复现
c语言·开发语言·数据结构·c++·算法·leetcode·list
嘿嘻哈呀2 天前
命令行解释器中shell、bash和zsh的区别
bash·shell·zsh·命令行解释器
莹莹学编程—成长记2 天前
list基础用法
数据结构·list
Darkwanderor2 天前
c++STL-list的使用和迭代器
c++·list
上天_去_做颗惺星 EVE_BLUE2 天前
Docker入门教程:常用命令与基础概念
linux·运维·macos·docker·容器·bash
打鱼又晒网2 天前
数据类型:List
数据结构·list
java程序员CC2 天前
记录为什么LIst数组“增删慢“,LinkedList链表“查改快“?
数据结构·链表·list