如何使用 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

答案2

根据您对基本命令的考虑,如果您的文件具有与所示完全相同的结构,grep可能会帮助您:

$ grep -B2 -A3 '"name" : "Admin_UserGroup"' File

但如果全部都在一行中:

$ sed -E 's/\},\s*\{/\},\n\{/g' File | grep  '"name" : "Admin_UserGroup"'

使用您原来的示例,您可以像这样使用我的解决方案:

$ echo "$Group_ID" | grep ...

答案3

您可以使用 JSON.awk:

awk -f JSON.awk -v file1.json file2.json

https://github.com/step-/JSON.awk

答案4

$ awk '{if($0~/"AdminUserGroup"/) {NR=NR-1;print $0; NR=NR+2;print $0; } }'file1

通过这种方式,我们可以打印搜索字符串附近的行信息。另外我认为我们可以将所有这些放入一个数组中,而不是打印它:

$ awk '{if($0~/"AdminUserGroup"/) {NR=NR-1;arr[$i]=(print $0);i=$((i+1)); \
    NR=NR+2; arr[$i]=(print $0); } }'file1.

然后我们可以使用数组详细信息。

相关内容