'Get JSON value using grep, sed, or awk
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!
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
