Some JSON:
{"theSecret":"flhiusdh4543sdfasf34sdf+fsfs/sdf43454=","foo":{"bar":41,"something":"hello world","else":"hi","date":20230101,"qux":0,"digest":"sfhusdf234jkkjuiui23kjkj/SFDF34SDSFDS="}}
Prettified:
{
"theSecret": "flhiusdh4543sdfasf34sdf+fsfs/sdf43454=",
"foo": {
"bar": 41,
"something": "hello world",
"else": "hi",
"date": 20230101,
"qux": 0,
"digest": "sfhusdf234jkkjuiui23kjkj/SFDF34SDSFDS="
}
}
I want the value of the theSecret
key.
I tried this:
$ echo "$json" | sed -nE 's/.*theSecret":"(.*)".*/\1/p' # (.*?) doesn't work
Which gives:
flhiusdh4543................4SDSFDS=
i.e. from theSecret
until the end of digest
. That's because sed lacks non-greedy quantifiers, so .*?
doesn't work.
How can I do this?
(This is in an alpine container, which has sed
, grep
and awk
. jq
and perl
are unavailable.)
Instead of using a non-greedy quantifier, use a pattern that doesn't match the terminating "
.
$ echo "$json" | sed -nE 's/.*theSecret":"([^"]*)".*/\1/p' # (.*?) doesn't work