prabodhprakash prabodhprakash - 1 year ago 177
Bash Question

extract substring using regex in shell script

The strings could be of form:




I need help in writing regex that extract all the string inside $(..)

I created a regex like
but when I try to execute in shell script, it gives me error of unmatched parenthesis.

This is what I executed:

echo "$(sdfsdfdsf)"|grep -P '([(])\w+([)])' -o

I need to get all matching substrings.

Answer Source

Your question specifies "shell", but not "bash". So I'll start with a common shell-based tool (awk) rather than assuming you can use any particular set of non-POSIX built-ins.

$ cat inp.txt$(PRODUCT_NAME:rfc1034identifier)

$ awk -F'[()]' '{for(i=2;i<=NF;i+=2){print $i}}' inp.txt


This awk one-liner defines a field separator that consists of opening or closing brackets. With such a field separator, every even-numbered field will be the content you're looking for, assuming all lines of input are correctly formatted and there are no parentheses embedded inside other parentheses.

If you did want to do this in POSIX shell alone, the following would be an option:


while read line; do
  while expr "$line" : '.*(' >/dev/null; do
    echo "${line%%)*}"
done < inp.txt

This steps through each line of input, slicing it up using the parentheses and printing each slice. Note that this uses expr, which most likely an external binary, but is at least included in POSIX.1.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download