【问题标题】:How can I extract only the name field from json by using gcloud command?如何使用 gcloud 命令仅从 json 中提取名称字段?
【发布时间】:2021-01-03 08:40:32
【问题描述】:

我是 gcp 和 linux 的新手。我正在尝试使用gcloud 命令从json 格式中获取名称。 下面是命令和输出:

gcloud firestore indexes composite list --format=json

输出:

[
  {
    "fields": [
      {
        "fieldPath": "is_active",
        "order": "ASCENDING"
      },
      {
        "fieldPath": "created_at",
        "order": "DESCENDING"
      },
      {
        "fieldPath": "__name__",
        "order": "ASCENDING"
      }
    ],
    "name": "projects/emc-ema-simcs-d-286404/databases/(default)/collectionGroups/customer/indexes/CICAgNjbhowK",
    "queryScope": "COLLECTION",
    "state": "READY"
  }

如何使用 gcloud 命令获取 name 值。 我尝试使用:gcloud firestore indexes composite list | grep name | awk -F'name: ' '{print $2}',但这没有给出任何输出。

请帮忙!

谢谢 英式

【问题讨论】:

    标签: json bash shell awk terminal


    【解决方案1】:

    最后,我能够使用以下命令获取 name 值:

    gcloud firestore indexes composite list --format=json | grep '"name"' | awk -F' ' '{print $2}' | awk -F'"' '{print $2}'

    输出:

    projects/emc-ema-simcs-d-xxxxxx/databases/(default)/collectionGroups/customer/indexes/CICAgNjbhowK
    

    【讨论】:

    • 当您使用awk 时,您永远不需要grep,也不需要将awk 输出到另一个awk,并且-F' 'FS 设置为默认值它已经拥有的价值。 grep '"name"' | awk -F' ' '{print $2}' | awk -F'"' '{print $2}' = awk '/"name"/{split($2,f,/"/); print f[2]}' 可以缩写为 awk -F'"' '/"name"/{print $4}' 但无论如何你都应该使用 jq 来解析 JSON。
    【解决方案2】:

    假设您的工具输出真的在开始时没有不匹配的 [,这会使其无效 JSON,如您在问题中提供的示例中那样,而是同时具有 @ 987654322@ 和 ] 或两者都没有,即它是有效的 JSON。

    如果输出以[ 开头并以] 结尾,那么您将使用jq 作为:

    gcloud firestore indexes composite list --format=json | jq -r '.[0] .name'
    

    否则你会使用jq作为:

    gcloud firestore indexes composite list --format=json | jq -r '.name'
    

    例如,使用cat file<N> 代替我没有的gcloud firestore indexes composite list --format=json

    $ cat file1
    [
      {
        "fields": [
          {
            "fieldPath": "is_active",
            "order": "ASCENDING"
          },
          {
            "fieldPath": "created_at",
            "order": "DESCENDING"
          },
          {
            "fieldPath": "__name__",
            "order": "ASCENDING"
          }
        ],
        "name": "projects/emc-ema-simcs-d-286404/databases/(default)/collectionGroups/customer/indexes/CICAgNjbhowK",
        "queryScope": "COLLECTION",
        "state": "READY"
      }
    ]
    $
    $ cat file1 | jq -r '.[0] .name'
    projects/emc-ema-simcs-d-286404/databases/(default)/collectionGroups/customer/indexes/CICAgNjbhowK
    

    $ cat file2
      {
        "fields": [
          {
            "fieldPath": "is_active",
            "order": "ASCENDING"
          },
          {
            "fieldPath": "created_at",
            "order": "DESCENDING"
          },
          {
            "fieldPath": "__name__",
            "order": "ASCENDING"
          }
        ],
        "name": "projects/emc-ema-simcs-d-286404/databases/(default)/collectionGroups/customer/indexes/CICAgNjbhowK",
        "queryScope": "COLLECTION",
        "state": "READY"
      }
    $
    $ cat file2 | jq -r '.name'
    projects/emc-ema-simcs-d-286404/databases/(default)/collectionGroups/customer/indexes/CICAgNjbhowK
    

    【讨论】:

    • 谢谢埃德。我想从 json 中获取名称列表,所以使用了你的命令。 gcloud firestore indexes composite list --format=json | jq -r '.name'感谢您的帮助
    猜你喜欢
    • 2021-01-06
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    相关资源
    最近更新 更多