使用 jq 提取特定的 属性 值并在单行上输出
Using jq to extract specific property values and output on a single line
我有这个 ElasticSearch 快照输出,我想将它减少到在一行上打印,每个 snapshot
end_time_in_millis
和 snapshot
属性 由 space:
分隔
1429609790767 snapshot_1
1429681169896 snapshot_2
基本上是
的输出
cat data | jq '.snapshots[].end_time_in_millis'
和
cat data | jq '.snapshots[].snapshot'
但合并在一条线上。
我正在查看 map
但不知道如何应用它;也在阅读 我试过了:
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
选项来获取原始输出。
我有这个 ElasticSearch 快照输出,我想将它减少到在一行上打印,每个 snapshot
end_time_in_millis
和 snapshot
属性 由 space:
1429609790767 snapshot_1
1429681169896 snapshot_2
基本上是
的输出cat data | jq '.snapshots[].end_time_in_millis'
和cat data | jq '.snapshots[].snapshot'
但合并在一条线上。
我正在查看 map
但不知道如何应用它;也在阅读
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
选项来获取原始输出。