gpt4 book ai didi

arrays - 使用带有内联输出的 jq 1.3 计算 JSON 数组的分组成员

转载 作者:行者123 更新时间:2023-12-01 12:07:07 25 4
gpt4 key购买 nike

我有一个 JSON 文件:

[
{
"instance_AZs": [
"us-east-1b",
"us-east-1b",
"us-east-1b",
"us-east-1c",
"us-east-1c",
"us-east-1a",
"us-east-1a",
"us-east-1c",
"us-east-1b",
"us-east-1a",
"us-east-1a",
"us-east-1c",
"us-east-1a",
"us-east-1b",
"us-east-1b",
"us-east-1c",
"us-east-1c",
"us-east-1b"
],
"ASGname": "myapp"
}
]

期望的输出是:

myapp.us-east-1a.5
myapp.us-east-1b.7
myapp.us-east-1c.6

就我可以使用 jq 1.3 语法而言,我编写了以下代码:

cat file.json | jq -r '.[] | .ASGname, (.instance_AZs | group_by(.) | map(. | length , (. | unique ) [] ))'

产生输出:

myapp
[
5,
"us-east-1a",
7,
"us-east-1b",
6,
"us-east-1c"
]

能否请您建议如何生成所需的输出?

最佳答案

这是一种方法:

jq -r '.[]|.ASGname as $g|.instance_AZs|group_by(.)[]|"\($g).\(.[0]).\(length)"' file.json

关于arrays - 使用带有内联输出的 jq 1.3 计算 JSON 数组的分组成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55489052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com