I found this answer: Group matching with grep includes extra characters but the anwser doesn't work for me.
I have a line in a json file that looks like this:
"object.0.1_foo": {
I want to match only the :
in this case, other times I want to match the {
.
grep -Eo -m 1 '[\:\{\}\[\]' <<<$object
returns : {
It was my understanding that characters inside brackets are the only thing searched for and if I ask for only the first match I should get just :
It seems like grep is treating \:\{
as a single 'character'. What's really odd to me is if I use this regex:
grep -Eo -m 1 '[^\s][\:\{\}\[\]' <<<$object
to exclude the space trying to force grep to see : and { as two distinct characters it returns
":
which is really confusing to me because the "
is not in my regex at all.
I can't figure out how to adjust my regex to only get the single match.
grep
on JSON content. JSON is structured you should leverage that with appropriate tools understanding JSON, likejq
.