From a pattern such as
[string 1]{string 2}
I want to extract string 2
, the string between the last pair of matching curly braces -- that is delete [string 1]
and the open {
and close }
. My attempt below breaks when there is a additional [
, ]
pairs in either string 1
or string 2
.
Desired Output:
The desired output from the script below begins with foo
and ends with a digit
:
foo bar 1
foo bar 2
foo[3]{xyz} bar 3
foo $sq[3]{xyz}$ bar 4
foo $sq[3]{xyz}$ bar 5
foo $sq[3]{xyz}$ bar 6
foo $sq[3]{xyz}$ bar 7
foo $sq[3]{xyz}$ bar 8'
foo $sq[abc]{xyz}$ bar 9'
foo $sq[abc]{xyz}$ bar 10'
Assumptions:
- Parameter to
RemoveInitialSquareBraces
always begins with a[
and ends with a}
. - The opening
[
forstring 1
will have a matching]
at the point where the opening{
begins forstring 2
.
Platform:
- MacOS 10.9.5
Script
#!/bin/bash
function RemoveInitialSquareBraces {
#EXTRACTED_TEXT="$(\
# echo "$1" \
# | sed 's/^\[.*\]//' \
# | sed 's/{//' \
# | sed 's/}$//' \
# )"
EXTRACTED_TEXT="$(\
echo "$1" \
| sed 's/.*[^0-9]\]{\(.*\)}/\1/' \
)"
echo "${EXTRACTED_TEXT}"
}
RemoveInitialSquareBraces '[]{foo bar 1}'
RemoveInitialSquareBraces '[abc]{foo bar 2}'
RemoveInitialSquareBraces '[]{foo[3]{xyz} bar 3}'
RemoveInitialSquareBraces '[]{foo $sq[3]{xyz}$ bar 4}'
RemoveInitialSquareBraces '[goo{w}]{foo $sq[3]{xyz}$ bar 5}'
RemoveInitialSquareBraces '[goo[3]{w}]{foo $sq[3]{xyz}$ bar 6}'
RemoveInitialSquareBraces '[goo[3]{w} hoo[3]{5}]{foo $sq[3]{xyz}$ bar 7}'
RemoveInitialSquareBraces '[goo[3]{w} hoo[3]{5}]{foo $sq[3]{xyz}$ bar 8}'
RemoveInitialSquareBraces '[goo[3]{w} hoo[xyz]{5}]{foo $sq[abc]{xyz}$ bar 9}'
RemoveInitialSquareBraces '[goo[3]{w} hoo[xyz]{uvw}]{foo $sq[abc]{xyz}$ bar 10}'
exit 0
sed
or are you open to other tools? If so, do you haveperl
installed?sed
solution. But if it turns out too difficult to do withsed
ten I'll consider alternate solutions.