gpt4 book ai didi

json - 使用jq提取特定的属性值并在一行上输出

转载 作者:行者123 更新时间:2023-12-04 01:51:52 26 4
gpt4 key购买 nike

我有这个ElasticSearch快照输出,并希望减少它以便从每个snapshot一行打印出来,end_time_in_millissnapshot属性的值用空格分开:

1429609790767 snapshot_1
1429681169896 snapshot_2

基本上是输出
  • cat data | jq '.snapshots[].end_time_in_millis'
  • cat data | jq '.snapshots[].snapshot'

  • 但结合在一起。

    我当时在看 map,但无法确定如何应用它。还读了 this answer我试过了:
    cat data  | jq '.snapshots[] | map(. |= with_entries( select( .key == ( "snapshot") ) ) )'

    但是,这会产生很多错误和 null输出。

    数据:
    {
    "snapshots": [
    {
    "shards": {
    "successful": 1,
    "failed": 0,
    "total": 1
    },
    "failures": [],
    "snapshot": "snapshot_1",
    "indices": [
    "myindex1"
    ],
    "state": "SUCCESS",
    "start_time": "2015-04-21T09:45:47.041Z",
    "start_time_in_millis": 1429609547041,
    "end_time": "2015-04-21T09:49:50.767Z",
    "end_time_in_millis": 1429609790767,
    "duration_in_millis": 243726
    },
    {
    "shards": {
    "successful": 1,
    "failed": 0,
    "total": 1
    },
    "failures": [],
    "snapshot": "snapshot_2",
    "indices": [
    "myindex1"
    ],
    "state": "SUCCESS",
    "start_time": "2015-04-22T05:36:02.333Z",
    "start_time_in_millis": 1429680962333,
    "end_time": "2015-04-22T05:39:29.896Z",
    "end_time_in_millis": 1429681169896,
    "duration_in_millis": 207563
    }
    ]
    }

    最佳答案

    使用此过滤器:

    .snapshots[] | "\(.end_time_in_millis) \(.snapshot)"

    这将为每个快照建立一个字符串,该字符串包含结束时间和快照名称。

    只要确保您使用 -r选项来获取原始输出即可。

    关于json - 使用jq提取特定的属性值并在一行上输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29982897/

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