将jq与bash结合使用以对数组中的每个对象运行命令 [英] Using jq with bash to run command for each object in array

查看:37
本文介绍了将jq与bash结合使用以对数组中的每个对象运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jq为JSON数组中的每个JSON对象运行Bash命令?到目前为止,我有这个:

How can I run a Bash command for every JSON object in a JSON array using jq? So far I have this:

cat credentials.json | jq -r '.[] | .user, .date, .email' | mycommand -u {user} -d {date} -e {email}

这似乎不起作用.如何将参数从JSON数组中取出到命令中?

This doesn't seem to work. How can I take the parameters out of the JSON array into my command?

我的JSON文件如下所示:

My JSON file looks something like this:

[
   "user": "danielrvt",
   "date": "11/10/1988",
   "email": "myemail@domain.com",
   ...
]

推荐答案

您最好的选择是将每条记录以TSV格式输出,然后从shell循环中读取.

Your best bet is probably to output each record in something like TSV format, then read that from a shell loop.

jq -r '.[]|[.user, .date, .email] | @tsv' |
  while IFS=$'\t' read -r user date email; do
    mycommand -u "$user" -d "$date" -e "$email"
  done

jq本身没有像system那样从过滤器内部运行外部命令的东西,尽管看起来

jq itself doesn't have anything like a system call to run an external command from within a filter, although it seems that they are working on it.

这篇关于将jq与bash结合使用以对数组中的每个对象运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆