I need to replace all instances of a character (period in my case) in 1+ portions/segments/ranges of a string. I'm using Bash on Linux. Ideally the solution is in Bash, but if it's either not possible or terribly complex I can call any app commonly found on Linux (sed, Python, etc).
Example:
Starting String: "<mark>foo.bar.baz</mark> blah. blah. blah. <mark>abc.def.ghi</mark> ..." .
Needed transformation: Replace all periods "." between <mark> and </mark> with the string "<wbr />" .
Desired Result: "<mark>foo<wbr />bar<wbr />baz</mark> blah. blah. blah. <mark>abc<wbr />def<wbr />ghi</mark>" .
EDITS:
The starting string will never contain <mark> or </mark> within a set of them (ie. the range markers are never nested).
I'm asking for help with some built-in Bash capability to perform this. The obvious mechanism is to try to find and , and then perform substitution in the content between. I know Bash can do offset finding (in an indirect way), and substitution. But can it be performed on a subset?
For the comments regarding parsing this as XML: I did not say this is XML so you should not assume it. Ultimately it's irrelevant to my question; the range markers can be anything.
Here's something I got working. It's not pure Bash, but it's simple.
while $(echo "${my_str}" | grep -E '<mark>[^.]*\.[^<]*</mark>' >/dev/null 2>&1) ; do
my_str=$(echo "${my_str}" | sed -E -e 's,(<mark>[^.]*)\.([^<]*</mark>),\1<wbr />\2,g')
done
tmp=$string; newstr=; while [[ $tmp == *'<mark>'*'</mark>'* ]]; do tmp2=${tmp#*<mark>*</mark>}; tmp3=${tmp%"$tmp2"}; tmp=$tmp2; tmp4=${tmp3%%<mark>*</mark>}; tmp5=${tmp3#"$tmp4"}; tmp5=${tmp5//./'<wbr />'}; tmp5="<begin>${tmp5#<mark>}"; tmp5="${tmp5%</mark>}</end>"; newstr+=$tmp4$tmp5; done; newstr+=$tmp; printf '%s\n' "$newstr"while $(echo ... | grep ...); dowithwhile grep -q -E '<mark>[^.]*\.[^<]*</mark>' <<< "${my_str}"; doto eliminate two subshell calls on each pass through the loop; the$(echo ... | sed ...)could be replaced with$(sed ... <<< "${my_str}")to eliminate another subshell, while this last subshell could be replaced with some creative parameter substitutions; though I'd look into how to compare${my_str}to a regex and how that populates theBASH_REMATCH[]array, then theBASH_REMATCH[]results can be used to formulate the parameter substitution