regex - Getting JSON value from JSON String using Shell Script -
i have json string:
{"name":"http://someurl/ws/someid","id":"someid"}
i want value "id" key , store in variable. succesfully tried using jq. due constraints, need achieve using grep , string matching.
i tried far: grep -po '"id":.*?[^\\]"';
giving "id":"ws-4c906698-03a2-49c3-8b3e-dea829c7fdbe"
output. need id value. please help
with pcre regex, may use lookarounds. thus, need put "id":"
positive lookbehind construct, , match 1 or more chars other "
:
grep -po '(?<="id":")[^"]+'
where
(?<="id":")
- requires"id":"
appear left of current position (but matched text not added match value) and[^"]+
- matches , adds match 1 or more chars other"
.
to values escaped quotes:
grep -po '(?<="id":")[^"\\]*(?:\\.[^"\\]*)*'
here, (?<="id":")
still match position right after "id":"
, following matched:
[^"\\]*
- 0 or more chars other"
,\
(?:\\.[^"\\]*)*
- 0 or more consequent sequences of:\\.
-\
, char (any escape sequence)[^"\\]*
- 0 or more chars other"
,\
Comments
Post a Comment