bash - Regex to extract values between brackets -
i've looked @ few threads on , can't seem working. issue regex statement and/or bash_rematch.
there ever max of 4 x ()'s
have following bash script:
#!/bin/bash brackets_regex="\((.*?)\)" text="random date (entry1) more random data (entry2) random (entry3) random data (entry4)" if [[ $text =~ $brackets_regex ]]; echo ${bash_rematch[0]}; echo ${bash_rematch[1]}; echo ${bash_rematch[2]}; echo ${bash_rematch[3]}; fi
expected output should be:
entry1 entry2 entry3 entry4
current output:
(entry1) more random data (entry2) random (entry3) random data (entry4) entry1) more random data (entry2) random (entry3) random data (entry4
using gnu grep:
grep -op '\(\k[^)]*' <<< "$text" entry1 entry2 entry3 entry4
using gnu-awk:
text="random date (entry1) more random data (entry2) random (entry3) random data (entry4)" awk -v fpat='\\([^)]*\\)' '{for(i=1; i<=nf; i++) {gsub(/[()]/, "", $i); print $i}}' <<< "$text" entry1 entry2 entry3 entry4
Comments
Post a Comment