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.

  • "I have a line in a json file that looks like this:" You shouldn't use grep on JSON content. JSON is structured you should leverage that with appropriate tools understanding JSON, like jq. Mar 23, 2021 at 17:26
  • I'm just playing around with bash trying learn more about arrays and redirection. I realize bash is terrible tool for parsing JSON, but I thought it would be interesting to come up with something that didn't require an external tool. There are a couple scripts out there that do it and I'm seeing if I can come up with something different with my terrible bash skills.
    – spence
    Mar 24, 2021 at 13:21

1 Answer 1


-m 1 says to stop after 1 matching line; since there are two matching characters, -o will print them both (although it should be on two separate lines).

In addition, characters are literal inside [..] so [^\s] is matching any single character that is not \ or s. In your case, that is the leading ".

If you really want to return only the :, then all you need is grep -o ':'

  • That is good to know about 1 matching line, I assumed it was just the match. Your answer explains everything. Thank you.
    – spence
    Mar 22, 2021 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.