通过命令行获取包含多个 JSON 的数组中包含的键的值

发布于 2025-01-10 14:05:08 字数 3099 浏览 0 评论 0原文

我有一个数组,里面包含很多 json 。我想在该数组中搜索与包含名称的 json 位于同一位置的 uuid 值

"name":"120GB"

我需要一些实际查看数组内部并获取 uuid 值的东西。我不知道这是否可以通过Linux 命令行实现。我想了很久,不知道如何下手。

这是数组,有时包含 "name:120GB" 的 json 位于第一位,有时位于第二位,等等。

这里是数组:

  [
  {
    "uuid": "c70c1627-71ec-46c8-8ce1-43055adcb606",
    "name": "DESCARGAS",
    "comment": "",
    "mntentref": "7be7b356-177a-4436-802a-2e7b785d52b6",
    "reldirpath": "DESCARGAS/",
    "privileges": "",
    "_used": true,
    "device": "6TBNAS",
    "description": "DESCARGAS [on 6TBNAS, DESCARGAS/]",
    "mntent": {
      "devicefile": "/dev/sdc1",
      "fsname": "/dev/disk/by-uuid/c8175ed0-e2a8-473f-846c-36e5dc79e4bd",
      "dir": "/srv/dev-disk-by-uuid-c8175ed0-e2a8-473f-846c-36e5dc79e4bd",
      "type": "ext4",
      "posixacl": true
    }
  },
  {
    "uuid": "70593bb6-0876-436c-bdd7-3b401fb55d50",
    "name": "MUSICA",
    "comment": "",
    "mntentref": "6c59b572-faf0-4682-85e8-ad92eaa6d34a",
    "reldirpath": "MUSICA/",
    "privileges": "",
    "_used": true,
    "device": "3TBNAS",
    "description": "MUSICA [on 3TBNAS, MUSICA/]",
    "mntent": {
      "devicefile": "/dev/sdb1",
      "fsname": "/dev/disk/by-uuid/2cac8ffa-bca4-4564-904f-cae65e56b650",
      "dir": "/srv/dev-disk-by-uuid-2cac8ffa-bca4-4564-904f-cae65e56b650",
      "type": "ext4",
      "posixacl": true
    }
  },
  {
    "uuid": "f0a0aa62-2a97-4cee-86d9-55961a675978",
    "name": "NINA",
    "comment": "",
    "mntentref": "6c59b572-faf0-4682-85e8-ad92eaa6d34a",
    "reldirpath": "NINA/",
    "privileges": "",
    "_used": true,
    "device": "3TBNAS",
    "description": "NINA [on 3TBNAS, NINA/]",
    "mntent": {
      "devicefile": "/dev/sdb1",
      "fsname": "/dev/disk/by-uuid/2cac8ffa-bca4-4564-904f-cae65e56b650",
      "dir": "/srv/dev-disk-by-uuid-2cac8ffa-bca4-4564-904f-cae65e56b650",
      "type": "ext4",
      "posixacl": true
    }
  },
  {
    "uuid": "7fe35e3b-8cbe-43f3-ad72-d0710ce6e0e7",
    "name": "COMPARTIR",
    "comment": "",
    "mntentref": "7be7b356-177a-4436-802a-2e7b785d52b6",
    "reldirpath": "COMPARTIR/",
    "privileges": "",
    "_used": true,
    "device": "6TBNAS",
    "description": "COMPARTIR [on 6TBNAS, COMPARTIR/]",
    "mntent": {
      "devicefile": "/dev/sdc1",
      "fsname": "/dev/disk/by-uuid/c8175ed0-e2a8-473f-846c-36e5dc79e4bd",
      "dir": "/srv/dev-disk-by-uuid-c8175ed0-e2a8-473f-846c-36e5dc79e4bd",
      "type": "ext4",
      "posixacl": true
    }
  },
  {
    "uuid": "abbaca32-09a2-410b-9918-dd1d0ee66273",
    "name": "120GB",
    "comment": "",
    "mntentref": "1157e369-73a4-4c61-b8d2-7cbccc207a11",
    "reldirpath": "/",
    "privileges": "",
    "_used": false,
    "device": "120GB_ANTIGUO",
    "description": "120GB [on 120GB_ANTIGUO, /]",
    "mntent": {
      "devicefile": "/dev/sdd1",
      "fsname": "/dev/disk/by-uuid/269E8B449E8B0B8D",
      "dir": "/srv/dev-disk-by-uuid-269E8B449E8B0B8D",
      "type": "ntfs",
      "posixacl": false
    }
  }
]

如何获取 uuid 的值?我希望我已经很好地解释了自己。

I have an array that contains many json inside. I want to search within that array for the uuid value that is in the same position as the json that contains the name:

"name":"120GB"

I need something that actually looks inside the arrays and gets the value of uuid. I don't know if this will be possible through the Linux command line. I've been thinking about it for a long time and I don't know how to get my hands on it.

This is the array, sometimes the json containing the "name:120GB" is in the first place, sometimes in the second, etc.

Here the array:

  [
  {
    "uuid": "c70c1627-71ec-46c8-8ce1-43055adcb606",
    "name": "DESCARGAS",
    "comment": "",
    "mntentref": "7be7b356-177a-4436-802a-2e7b785d52b6",
    "reldirpath": "DESCARGAS/",
    "privileges": "",
    "_used": true,
    "device": "6TBNAS",
    "description": "DESCARGAS [on 6TBNAS, DESCARGAS/]",
    "mntent": {
      "devicefile": "/dev/sdc1",
      "fsname": "/dev/disk/by-uuid/c8175ed0-e2a8-473f-846c-36e5dc79e4bd",
      "dir": "/srv/dev-disk-by-uuid-c8175ed0-e2a8-473f-846c-36e5dc79e4bd",
      "type": "ext4",
      "posixacl": true
    }
  },
  {
    "uuid": "70593bb6-0876-436c-bdd7-3b401fb55d50",
    "name": "MUSICA",
    "comment": "",
    "mntentref": "6c59b572-faf0-4682-85e8-ad92eaa6d34a",
    "reldirpath": "MUSICA/",
    "privileges": "",
    "_used": true,
    "device": "3TBNAS",
    "description": "MUSICA [on 3TBNAS, MUSICA/]",
    "mntent": {
      "devicefile": "/dev/sdb1",
      "fsname": "/dev/disk/by-uuid/2cac8ffa-bca4-4564-904f-cae65e56b650",
      "dir": "/srv/dev-disk-by-uuid-2cac8ffa-bca4-4564-904f-cae65e56b650",
      "type": "ext4",
      "posixacl": true
    }
  },
  {
    "uuid": "f0a0aa62-2a97-4cee-86d9-55961a675978",
    "name": "NINA",
    "comment": "",
    "mntentref": "6c59b572-faf0-4682-85e8-ad92eaa6d34a",
    "reldirpath": "NINA/",
    "privileges": "",
    "_used": true,
    "device": "3TBNAS",
    "description": "NINA [on 3TBNAS, NINA/]",
    "mntent": {
      "devicefile": "/dev/sdb1",
      "fsname": "/dev/disk/by-uuid/2cac8ffa-bca4-4564-904f-cae65e56b650",
      "dir": "/srv/dev-disk-by-uuid-2cac8ffa-bca4-4564-904f-cae65e56b650",
      "type": "ext4",
      "posixacl": true
    }
  },
  {
    "uuid": "7fe35e3b-8cbe-43f3-ad72-d0710ce6e0e7",
    "name": "COMPARTIR",
    "comment": "",
    "mntentref": "7be7b356-177a-4436-802a-2e7b785d52b6",
    "reldirpath": "COMPARTIR/",
    "privileges": "",
    "_used": true,
    "device": "6TBNAS",
    "description": "COMPARTIR [on 6TBNAS, COMPARTIR/]",
    "mntent": {
      "devicefile": "/dev/sdc1",
      "fsname": "/dev/disk/by-uuid/c8175ed0-e2a8-473f-846c-36e5dc79e4bd",
      "dir": "/srv/dev-disk-by-uuid-c8175ed0-e2a8-473f-846c-36e5dc79e4bd",
      "type": "ext4",
      "posixacl": true
    }
  },
  {
    "uuid": "abbaca32-09a2-410b-9918-dd1d0ee66273",
    "name": "120GB",
    "comment": "",
    "mntentref": "1157e369-73a4-4c61-b8d2-7cbccc207a11",
    "reldirpath": "/",
    "privileges": "",
    "_used": false,
    "device": "120GB_ANTIGUO",
    "description": "120GB [on 120GB_ANTIGUO, /]",
    "mntent": {
      "devicefile": "/dev/sdd1",
      "fsname": "/dev/disk/by-uuid/269E8B449E8B0B8D",
      "dir": "/srv/dev-disk-by-uuid-269E8B449E8B0B8D",
      "type": "ntfs",
      "posixacl": false
    }
  }
]

How could I get the value of uuid? I hope I have explained myself well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听,心雨的声音 2025-01-17 14:05:08

您可以使用jq提取uuid:

cat file | jq '.[] | select(.name == "120GB") | {uuid}' 

示例输出:

{
  "uuid": "abbaca32-09a2-410b-9918-dd1d0ee66273"
}

仅uuid:

cat file | jq '.[] | select(.name == "120GB") | {uuid}' |awk -F: '{print $2}'

示例输出:

"abbaca32-09a2-410b-9918-dd1d0ee66273"

或者:

cat file | jq '.[] | select(.name == "120GB") | {uuid}' |awk -F'"' '{print $4}'

abbaca32-09a2-410b-9918-dd1d0ee66273

You can extract the uuid using jq:

cat file | jq '.[] | select(.name == "120GB") | {uuid}' 

sample output:

{
  "uuid": "abbaca32-09a2-410b-9918-dd1d0ee66273"
}

Only the uuid:

cat file | jq '.[] | select(.name == "120GB") | {uuid}' |awk -F: '{print $2}'

sample output:

"abbaca32-09a2-410b-9918-dd1d0ee66273"

Or:

cat file | jq '.[] | select(.name == "120GB") | {uuid}' |awk -F'"' '{print $4}'

abbaca32-09a2-410b-9918-dd1d0ee66273
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文