【问题标题】:I need to transform the output of the jq comand into a table我需要将 jq 命令的输出转换为表格
【发布时间】:2020-01-16 17:52:14
【问题描述】:

我正在使用此命令从 Json 中提取一些信息。

#!/bin/bash

TOKEN=$(oc whoami -t)
ENDPOINT=$(oc config current-context | cut -d/ -f2 | tr - .)
NAMESPACE=$(oc config current-context | cut -d/ -f1)


curl -k \
    -H "Authorization: Bearer $TOKEN" \
    -H 'Accept: application/json' \
    https://$ENDPOINT/api/v1/resourcequotas |   jq -jr '[[.items[].metadata.namespace ], [.items[].metadata.creationTimestamp ], [.items[].spec.hard["requests.storage"]],[.items[].status.used["requests.storage"]],[.items[].spec.hard["limits.cpu"]],[.items[].spec.hard["limits.memory"]],[.items[].status.used["limits.cpu"]],[.items[].status.used["limits.memory"]]] ' > item.txt

当我执行脚本时,它会返回一个 txt 文件:

[
  [
    "glusterfs",
    "timpdv",
    "timpdv",
    "whatsappprd",
    "whatsappprd",
    "wso2ms",
    "wso2ms"
  ],
  [
    "2019-12-12T13:47:25Z",
    "2019-01-10T19:53:29Z",
    "2019-01-10T20:36:18Z",
    "2019-02-15T17:39:43Z",
    "2019-02-13T18:20:30Z",
    "2019-12-12T13:49:57Z",
    "2019-12-12T13:50:45Z"
  ],
  [
    "200Gi",
    null,
    "300Gi",
    null,
    "1150Gi",
    null,
    "200Gi"
  ],
  [
    "0",
    null,
    "68Gi",
    null,
    "1074Gi",
    null,
    "0"
  ],
  [
    null,
    "16",
    null,
    "192",
    null,
    "80",
    null
  ],
  [
    null,
    "192Gi",

我需要将这些数据作为表格返回,每个值都是一列。 例如。

namespace,        timestamp,              request.storage
 "glusterfs",    "2019-12-12T13:47:25Z",    "200Gi",
    "timpdv",    "2019-01-10T19:53:29Z",    null,
    "timpdv",    "2019-01-10T20:36:18Z",    "300Gi",

【问题讨论】:

  • 在 bash 中!?你是个虐待狂。
  • 我不是虐待狂 rs,我只需要完成 rsrs。我正在从 prometheus 中提取这些信息。

标签: json jq


【解决方案1】:

忽略标题行,看起来你想要的jq查询更像这样:

.items[]
| [.metadata.namespace,
   .metadata.creationTimestamp,
   .spec.hard["requests.storage"],
   .status.used["requests.storage"],
   .spec.hard["limits.cpu"],
   .spec.hard["limits.memory"],
   .status.used["limits.cpu"],
   .status.used["limits.memory"]]
| @tsv

对于 CSV,请使用 @csv 而不是 @tsv。无论哪种情况,您都可能希望使用 -r 选项调用 jq。

【讨论】:

  • 差不多就是这样,我唯一不明白的是为什么everithing有一个't' "glusterfs\t2019-12-12T13:47:25Z\t200Gi\t0\t\t\t \t"
  • \t 是选项卡的 JSON 表示形式。请查看更新。
  • 好吧,现在说得通了。谢谢,我差点放弃使用 jq
猜你喜欢
  • 1970-01-01
  • 2023-01-28
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
相关资源
最近更新 更多