【问题标题】:How can I print multiple values in one line by `jq`?如何通过`jq`在一行中打印多个值?
【发布时间】:2021-10-31 23:26:47
【问题描述】:

我有如下 json 数据:

{
    "Items": [
        {
            "id": {
                "S": "c921e4eb-5958-424a-ae3a-b9cada0d9481"
            },
            "type": {
                "S": "transaction.1612878877726"
            }
        },
        {
            "id": {
                "S": "355057f0-4327-49c7-979f-5a27410d81ba"
            },
            "type": {
                "S": "transaction.1612345630260"
            }
        },
        {
            "id": {
                "S": "664dc02f-0ad8-484a-98a5-a403beea775b"
            },
            "type": {
                "S": "transaction.1612164919232"
            }
        },
...
  ]
}

我想将 idtype 的值从 Items 数组中的每一项打印在一行中,例如

c921e4eb-5958-424a-ae3a-b9cada0d9481, transaction.1612878877726
355057f0-4327-49c7-979f-5a27410d81ba, transaction.1612345630260
...

我尝试了cat file | jq '.Items[].id.S, .Items[].type.S',但它在单独的行中打印idtype。 jq怎么实现呢?

【问题讨论】:

    标签: json shell jq


    【解决方案1】:

    我只会使用字符串操作,或者添加 3 个字符串:

    jq --raw-output '.Items[] | .id.S + ", " + .type.S' file
    

    或使用字符串插值:

    jq --raw-output '.Items[] | "\(.id.S), \(.type.S)"' file
    

    你可以try it here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-18
      • 2020-12-18
      • 1970-01-01
      • 2022-12-14
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多