【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变量
相关推荐
IMPYLH10 小时前
Linux 的 shuf 命令
linux·运维·服务器·bash
IMPYLH12 小时前
Linux 的 shred 命令
linux·运维·服务器·bash
IT摆渡者13 小时前
Linux 巡检脚本BASH
linux·运维·bash
NoSi EFUL1 天前
redis存取list集合
windows·redis·list
IMPYLH1 天前
Linux 的 sha384sum 命令
linux·运维·服务器·网络·bash·哈希算法
IMPYLH1 天前
Linux 的 sha512sum 命令
linux·运维·服务器·bash·哈希算法·散列表
维吉斯蔡1 天前
【Ubuntu】Fcitx 搜狗拼音无法在 VS Code 输入中文的修复方案
linux·vscode·ubuntu·bash
IMPYLH2 天前
【无标题】
linux·运维·服务器·网络·bash
IMPYLH2 天前
Linux 的 sha256sum 命令
linux·运维·服务器·网络·bash·哈希算法