awkまたはsedを使用してjsonオブジェクトから値を取得する方法

awkまたはsedを使用してjsonオブジェクトから値を取得する方法

私は次のJSONを持っています:

[root@mdfdevha1 ~]#  echo "$Group_ID"
[ {
  "id" : "e27206c0-aeb6-43db-acda-c4ba43233071",
  "name" : "A1",
  "path" : "/A1",
  "subGroups" : [ ]
}, {
  "id" : "89f3bd6a-33a9-4e02-9fe3-eae660c5a6cf",
  "name" : "Admin_UserGroup",
  "path" : "/Admin_UserGroup",
  "subGroups" : [ ]
}, {
  "id" : "cdc2bce5-c3bb-4b88-bdaf-d87b8bb6c644",
  "name" : "Group104",
  "path" : "/Group104",
  "subGroups" : [ ]
}, {
  "id" : "a0d749f2-ab6c-4c27-ad55-3357eaab9527",
  "name" : "Group105",
  "path" : "/Group105",
  "subGroups" : [ ]
}, {
  "id" : "fbf99c34-d50d-408b-8d19-9713f9af3e3a",
  "name" : "Group106",
  "path" : "/Group106",
  "subGroups" : [ ]
}, {
  "id" : "ebd8336f-4017-4fb1-8035-153ae1d9ba37",
  "name" : "Group201",
  "path" : "/Group201",
  "subGroups" : [ ]
}, {
  "id" : "38f4aef7-caf0-4430-9e61-1ae7026e872f",
  "name" : "Group202",
  "path" : "/Group202",
  "subGroups" : [ ]
}, {
  "id" : "436a0f4a-8b1b-4d7d-a014-fcec3513644e",
  "name" : "Group203",
  "path" : "/Group203",
  "subGroups" : [ ]
}, {
  "id" : "41962c5f-e7e9-4748-b81f-e3f1880b78de",
  "name" : "Sure_Groups",
  "path" : "/Sure_Groups",
  "subGroups" : [ {
    "id" : "593dfe69-1ed8-4649-bde4-a277166333f8",
    "name" : "Test1",
    "path" : "/Sure_Groups/Test1",
    "subGroups" : [ ]
  } ]
}, {
  "id" : "6856b69b-9113-46e1-90c6-f34548625278",
  "name" : "UG_1",
  "path" : "/UG_1",
  "subGroups" : [ ]
}, {
  "id" : "6496a0fe-b41f-4f0f-9eb9-5ef749c9130a",
  "name" : "UG_12",
  "path" : "/UG_12",
  "subGroups" : [ ]
}, {
  "id" : "71a5f5ae-bf91-4cdf-ab3c-c09ca15080d6",
  "name" : "UG_1456",
  "path" : "/UG_1456",
  "subGroups" : [ ]
}, {
  "id" : "385ea518-1d40-45f7-afcd-c0488ff02e97",
  "name" : "UG_26",
  "path" : "/UG_26",
  "subGroups" : [ {
    "id" : "a4064e3a-e2e3-47bb-99b8-9f7fadb0bc20",
    "name" : "Test1",
    "path" : "/UG_26/Test1",
    "subGroups" : [ ]
  } ]
}, {
  "id" : "9c5efedc-b901-4dcf-bbc8-8ddeaa5d84f7",
  "name" : "UG_266",
  "path" : "/UG_266",
  "subGroups" : [ ]
}, {
  "id" : "c5eb3064-752c-4f7c-b4f1-ac59f50397dd",
  "name" : "Usergroup_01",
  "path" : "/Usergroup_01",
  "subGroups" : [ ]
}, {
  "id" : "d39dc10c-558b-433e-82b4-e01a8f1d8998",
  "name" : "Usergroup_02",
  "path" : "/Usergroup_02",
  "subGroups" : [ ]
} ]

awk 特定のデータを使用または取得する方法sed。どこからデータをインポートする必要がありますかname="Admin_UserGroup"

編集#1

ありがとうフセインワタニ彼の答えの最終的な順序は次のとおりです。

$ /opt/keycloak/bin/kcadm.sh get groups -r T0_Realm > Group.json
$ GROUP_ID_TEMP=$(grep -B1 -A0 '"name" : "Admin_UserGroup"' Group.json)
$ GROUP_ID=$(echo $GROUP_ID_TEMP | cut -d : -f2 | awk -F\" '{print $2}')

ベストアンサー1

使用jq:

$ printf '%s\n' "$Group_ID" | jq '.[] | select(.name == "Admin_UserGroup")'
{
  "id": "89f3bd6a-33a9-4e02-9fe3-eae660c5a6cf",
  "name": "Admin_UserGroup",
  "path": "/Admin_UserGroup",
  "subGroups": []
}

これはname、キーが値に対応する配列内のすべてのオブジェクトを選択しますAdmin_UserGroup

おすすめ記事