I have a two line file that I'm trying to get some info out of for a bash script using sed.

# File Comment
PrefixForInformation {information to be captured}

I need to get the information between but not including the curly braces. I have the PCRE regex /{(.*)}/ or \s{([^}]*) that seems to work in Online Regex 101 but I can't get that over to a working sed configuration.

  • whats wrong with the goat? – mikeserv Nov 5 '15 at 21:43
  • 2
    IMDb – SonOfAMotherlessGoat Nov 5 '15 at 21:46
  • Tried sed -n 's/{\(.*\)}/\1/p' but that gets me everything except the braces, including the Prefix and I don't need that. – SonOfAMotherlessGoat Nov 5 '15 at 21:50
$ sed -n 's/.*{\(.*\)}.*/\1/p' file
information to be captured

How it works

  • -n

    This tells sed not to print anything unless we explicitly ask it to.

  • s/.*{\(.*\)}.*/\1/p

    This substitute command captures as group 1 everything between two curly braces. The whole line is replaced with group 1, denoted \1. The p at the end tells sed that, if a match was made, it should print the result.

| improve this answer | |
  • Any suggestions for why the anonymous downvote? – John1024 Nov 5 '15 at 22:15
  • I tried to upvote, but I still only have 13 rep. I was wondering about that downvote? – SonOfAMotherlessGoat Nov 5 '15 at 22:17
  • @John1024, dunno. it happens sometimes. its a good answer so here's a +1 from me. – cas Nov 5 '15 at 22:48

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.