使用jq分配多个输出变量 [英] using jq to assign multiple output variables

查看:556
本文介绍了使用jq分配多个输出变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jq来解析 TVDB api 中的信息.我需要提取几个字段并将值分配给可以在我的bash脚本中继续使用的变量.我知道我可以使用variable="$(command)"通过bash轻松地将输出分配给一个变量,但是我需要输出来产生多个变量,并且我不想使用多个命令.

I am trying to use jq to parse information from the TVDB api. I need to pull a couple of fields and assign the values to variables that I can continue to use in my bash script. I know I can easily assign the output to one variable through bash with variable="$(command)" but I need the output to produce multiple variables and I don't want to make to use multiple commands.

我阅读了此文档:

https://stedolan.github.io/jq/manual/v1 .5/#Advancedfeatures

但是我不知道这是否与我想做的事情有关.

but I don't know if this relevant to what I am trying to do.

jq '.data'产生以下输出:

[
  {
    "absoluteNumber": 51,
    "airedEpisodeNumber": 6,
    "airedSeason": 4,
    "airedSeasonID": 680431,
    "dvdEpisodeNumber": 6,
    "dvdSeason": 4,
    "episodeName": "We Will Rise",
    "firstAired": "2017-03-15",
    "id": 5939660,
    "language": {
      "episodeName": "en",
      "overview": "en"
    },
    "lastUpdated": 1490769062,
    "overview": "Clarke and Roan must work together in hostile territory in order to deliver an invaluable asset to Abby and her team."
  }
]

我尝试了jq '.data | {episodeName:$name}'jq '.data | .episodeName as $name'只是为了尝试使之工作.我不了解文档,即使我正在寻找它也是如此.有办法做我想做的事吗?

I tried jq '.data | {episodeName:$name}' and jq '.data | .episodeName as $name' just to try and get one working. I don't understand the documentation or even if it's what I'm looking for. Is there a way to do what I am trying to do?

推荐答案

您可以在read中使用单独的变量:

You can use separate variables with read :

read var1 var2 var3 < <(echo $(curl -s 'https://api.github.com/repos/torvalds/linux' | 
     jq -r '.id, .name, .full_name'))

echo "id        : $var1"
echo "name      : $var2"
echo "full_name : $var3"

使用数组:

read -a arr < <(echo $(curl -s 'https://api.github.com/repos/torvalds/linux' | 
     jq -r '.id, .name, .full_name'))

echo "id        : ${arr[0]}"
echo "name      : ${arr[1]}"
echo "full_name : ${arr[2]}"

此外,您还可以将输出拆分为一些字符:

Also you can split jq output with some character :

IFS='|' read var1 var2 var3 var4 < <(curl '......' | jq -r '.data | 
    map([.absoluteNumber, .airedEpisodeNumber, .episodeName, .overview] | 
    join("|")) | join("\n")')

或使用类似数组:

set -f; IFS='|' data=($(curl '......' | jq -r '.data | 
    map([.absoluteNumber, .airedEpisodeNumber, .episodeName, .overview] | 
    join("|")) | join("\n")')); set +f

absoluteNumberairedEpisodeNumberepisodeName& overview分别是${data[0]}${data[1]}${data[2]}${data[3]}. set -fset +f用于分别禁用&启用 globbing .

absoluteNumber, airedEpisodeNumber, episodeName & overview are respectively ${data[0]}, ${data[1]}, ${data[2]}, ${data[3]}. set -f and set +f are used to respectively disable & enable globbing.

对于部分的问题,所有必填项字段使用join("|")

For the jq part, all your required fields are mapped and delimited with a '|' character with join("|")

如果您使用的是jq< 1.5,您必须使用tostring将每个Number字段的Number转换为String,例如:

If your are using jq < 1.5, you'll have to convert Number to String with tostring for each Number fields eg:

IFS='|' read var1 var2 var3 var4 < <(curl '......' | jq -r '.data | 
    map([.absoluteNumber|tostring, .airedEpisodeNumber|tostring, .episodeName, .overview] | 
    join("|")) | join("\n")')

这篇关于使用jq分配多个输出变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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