按jq中的值进行复杂排序和输出

问题描述 投票:0回答:1

我正在尝试获得线程兄弟姐妹的人类可读表示。

我有以下输出:输出

undercloud) [stack@undercloud ~]$ os baremetal introspection data save compute0 |jq '.numa_topology.cpus[] | select(.numa_node == 0) | .thread_siblings'
[
  11,
  51
]
[
  9,
  49
]
[
  58,
  18
],

....
...
..
.

(undercloud) [stack@undercloud ~]$

共同观点: 共同观点。

(undercloud) [stack@undercloud ~]$ os baremetal introspection data save compute0 | jq '.numa_topology.cpus'
[
  {
    "thread_siblings": [
      11,
      51
    ],
    "cpu": 11,
    "numa_node": 0
  },
  {
    "thread_siblings": [
      9,
      49
    ],
    "cpu": 9,
    "numa_node": 0
  },
  {
    "thread_siblings": [
      58,
      18
    ],
    "cpu": 18,
    "numa_node": 0
  },
....
...
..
.
(undercloud) [stack@undercloud ~]$

我想获得下一个输出:

0, 1, 2, 3, 4, ... etc
40 41 42 43 44 ... etc

是否可以使用

jq
获得这样的列表?也许需要后续的
awk/sed
处理。

P.S.:JSON 移至 Pastebin,因为 stackoverflow 引擎认为我的帖子细节很少,代码较多,但我认为我给出了一个非常具体的问题,并且它不属于 XY 问题。

json jq
1个回答
0
投票

这是一种对所有数字进行排序,然后通过查找间隙对其进行分区,并使用粘合字符串连接每个分区的方法:

jq -r '
  [.numa_topology.cpus[] | select(.numa_node == 0).thread_siblings[]]
  | [sort | .[1:], .[:1]] | until(first == [];
      if first[0] == last[-1] + 1 then last += [first[0]] else . + [[first[0]]] end
      | first |= .[1:]
    )[1:][] | join(", ")
'
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59

演示

© www.soinside.com 2019 - 2024. All rights reserved.