使用 grep、sed 或 awk 获取 JSON 值

发布于 2025-01-19 22:46:46 字数 387 浏览 2 评论 0原文

我有一个JSON配置文件,我正在尝试通过以下GREP命令提取值:

grep -oP '"private_key": "\K[^\"]*' config.json

如您所见,我有工作正则是REGEX,但是\ k语法在较旧版本的Regex中不支持。例如,如果将风味切换到Python或Ecmasixt,则正则是不再有效的。我想知道是否还有其他一些语法可以达到相同的结果。

https://regex101.com/r/jfgwnt/1 这个问题的更好名称,请让我知道!

I have a JSON configuration file I'm trying to extract values from via the following grep command:

grep -oP '"private_key": "\K[^\"]*' config.json

As you can see, I have working regex, however the \K syntax is not supported in older versions of Regex. For example, if you switch the flavor to Python or ECMAscript, the regex no longer works. I'm wondering if there's some other syntax to achieve the same result.

https://regex101.com/r/jfGwNT/1

Also, if anyone can think of a better name for this question, please let me know!

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

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

发布评论

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

评论(1

っ〆星空下的拥抱 2025-01-26 22:46:46

您可以使用 sed 来代替 gnu-grep,如下所示:

sed -nE 's/^ *"private_key": "([^"]+)".*/\1/p' file.json

-----BEGIN PRIVATE KEY-----\nMyKey\n-----END PRIVATE KEY-----\n

Instead of gnu-grep, you can use sed like this:

sed -nE 's/^ *"private_key": "([^"]+)".*/\1/p' file.json

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