本文介绍了使用jq提取特定的属性值并在一行上输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个ElasticSearch快照输出,并希望减少它以便在每个snapshot中从一行snapshotsnapshot属性的值中单独打印一个空格:

I've this ElasticSearch snapshot output and would like to reduce it to print, on a single line, from each snapshot the values of the end_time_in_millis and snapshot property separate by a space:

1429609790767 snapshot_1
1429681169896 snapshot_2

基本上是

  • cat data | jq '.snapshots[].end_time_in_millis'
  • cat data | jq '.snapshots[].snapshot'
  • cat data | jq '.snapshots[].end_time_in_millis' and
  • cat data | jq '.snapshots[].snapshot'

但合并成一行.

我当时在看map,但无法确定如何应用它.还阅读了此答案我尝试过的: /p>

I was looking at map but couldn't make out how to apply it; also reading this answer I tried:

cat data  | jq '.snapshots[] | map(. |= with_entries( select( .key == ( "snapshot") ) ) )'

但是这会产生很多错误并输出null.

But that produces lots of errors and null output.

数据:

{
  "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)"

这将为每个快照建立一个字符串,其中包括结束时间和快照名称.

This builds up a string for each of the snapshots consisting of the end time and the snapshot name.

只需确保使用-r选项即可获取原始输出.

Just make sure you use the -r option to get the raw output.

这篇关于使用jq提取特定的属性值并在一行上输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 05:58