【问题标题】:print group type once and all values in shell打印组类型一次并在 shell 中打印所有值
【发布时间】:2021-09-18 19:38:42
【问题描述】:

我使用以下命令将 stickyType 的 curl 响应分组:

curl <url> | jq 'group_by(.stickyType)' 它给出了以下输出:

[
  [
    {
      "stickyType": "TypeA",
      "content": "X",
    },
    {
      "stickyType": "TypeA",
      "content": "Y",
    }
  ],
  [
    {
      "stickyType": "TypeB",
      "content": "Z",
    }
  ]
]

现在我想将其打印为:

TypeA
1. X
2. Y

TypeB
1. Z

我该怎么做?

【问题讨论】:

  • 不需要编号。
  • 你能分享整个json吗?

标签: json group-by jq


【解决方案1】:

假设显示的示例 JSON 已更正,并且您使用 -r 命令行选项:

  • 无编号
    .[] | (.[0]|.stickyType), (.[] | .content)
  • 带编号:
    .[] | (.[0]|.stickyType), (range(0;length) as $i | "\($i). \(.[$i].content)")

例如,您可以将其中任何一个添加到您的 jq 管道中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 2021-10-11
    • 2023-02-12
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 2021-11-29
    相关资源
    最近更新 更多