What you say you want to do is not what you say sed .
You want to insert the number in the tag or replace it, if any. What you're trying to say sed is to replace the span tag and its contents, if there is one or a number, with the value of the shell variable.
You also use many complex, annoying, and erosive sequences that are simply not needed.
Here is what you want:
sed -r -i -e 's|<span id="unlockedCount">([0-9]{0,})</span>|<span id="unlockedCount">'"${unlockedCount}"'</span>|g' index.html
Please note the differences:
- Added
-r to enable extended expressions without which your capture pattern would not work. - Use
| instead of / as a delimiter for substitution, to avoid the need for / . - Single quoting
sed statements to avoid having things from the shell. - The corresponding span tag is included in the replacement section so that it is not deleted.
- To expand the
unlockedCount variable, close the one-shot expression, and then open it again. - Omitted
cat | which is useless here.
I also used double quotes around the shell variable extension because it is good practice, but if it does not contain spaces, it is not necessary.
I did not, strictly speaking, add -r . The usual old sed will work if you say \([0-9]\{0,\}\) , but the idea here was to simplify.
source share