简体   繁体   English

如何通过`jq`在一行中打印多个值?

[英]How can I print multiple values in one line by `jq`?

I have json data like below:我有如下 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"
            }
        },
...
  ]
}

I'd like to print the value id and type in one line per item from the Items array, eg我想打印值id并从Items数组中为每个项目type一行,例如

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

I tried cat file | jq '.Items[].id.S, .Items[].type.S'我试过cat file | jq '.Items[].id.S, .Items[].type.S' cat file | jq '.Items[].id.S, .Items[].type.S' but it prints id and type in separate lines. cat file | jq '.Items[].id.S, .Items[].type.S'但它在单独的行中打印idtype How can I achieve it with jq?我怎样才能用 jq 实现它?

I would just use string manipulation, either adding 3 strings :我只会使用字符串操作,或者添加 3 个字符串:

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

or using string interpolation :或使用字符串插值:

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

You can try it here .你可以在这里试试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM