小编Sup*_*DOS的帖子

在 MacOS 上匹配正则表达式并提取 json 中的所有实例

所以我有一个脚本,它返回一个 json 响应,其中包含一个键 Name 和一个文件名。

所以我设法让它在 GNU grep 中工作,但现在我需要它在 macOS 上运行,无需安装任何东西,而且 FBSD grep 不能使用 Perl 语法。知道如何做到这一点吗?

变量 files 包含 json-response。

这适用于 GNU grep:

files=($(echo $files | grep -oP '"Name":"\s*\K[^\s,]*(?=\s*",)'))
Run Code Online (Sandbox Code Playgroud)

返回 file01.jpg、file02.jpg 等并将其存储在数组中。

json 响应的示例在此处格式化,但在一行中:

    {
    "value": [
        {
            "Name": "file01.jpg",
            "TimeCreated": "2021-05-12T12:46:10Z",
            "TimeLastModified": "2021-05-12T12:46:10Z",
            "Title": null
        },
        {
            "Name": "file02.jpg",
            "TimeCreated": "2021-05-12T12:46:10Z",
            "TimeLastModified": "2021-05-12T12:46:10Z",
            "Title": null
        }
    ]
  }
Run Code Online (Sandbox Code Playgroud)

它在 files 变量中的显示方式:

{"value":[{"Name":"file01.jpg","TimeCreated":"2021-05-12T12:46:10Z","TimeLastModified":"2021-05-12T12:46:10Z","Title":null},{"Name":"file02.jpg","TimeCreated":"2021-05-12T12:46:10Z","TimeLastModified":"2021-05-12T12:46:10Z","Title":null}]}
Run Code Online (Sandbox Code Playgroud)

regex macos bash grep

1
推荐指数
2
解决办法
82
查看次数

标签 统计

bash ×1

grep ×1

macos ×1

regex ×1