Shell – sed regex for capture group between delimeters

command linelinuxregular expressionsedshell-script

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.

Best Answer

$ 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.

Related Question