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

Get value of a key contained in an array with several JSON by command line

我有一个数组,其中包含许多 json。我想在该数组中搜索 uuid 值 ,它与包含名称的 json 处于同一位置:

"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的值?我希望我已经解释清楚了。

您可以使用 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 }'

示例输出:

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

或:

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

abbaca32-09a2-410b-9918-dd1d0ee66273