Replacing HTML tag content using sed Replacing HTML tag content using sed bash bash

Replacing HTML tag content using sed


Try this:

sed -i -e "s/\(<span id=\"unlockedCount\">\)\(<\/span>\)/\1${unlockedCount}\2/g" index.html


What you say you want to do is not what you're telling sed to do.

You want to insert a number into a tag or replace it if present. What you're trying to tell sed to do is to replace a span tag and its contents, if any or a number, with the value of in a shell variable.

You're also employing a lot of complex, annoying and erorr-prone escape sequences which are just not necessary.

Here's what you want:

sed -r -i -e 's|<span id="unlockedCount">([0-9]{0,})</span>|<span id="unlockedCount">'"${unlockedCount}"'</span>|g' index.html

Note the differences:

  • Added -r to turn on extended expressions without which your capture pattern would not work.
  • Used | instead of / as the delimiter for the substitution so that escaping / would not be necessary.
  • Single-quoted the sed expression so that escaping things inside it from the shell would not be necessary.
  • Included the matched span tag in the replacement section so that it would not get deleted.
  • In order to expand the unlockedCount variable, closed the single-quoted expression, then later re-opened it.
  • Omitted cat | which was useless here.

I also used double quotes around the shell variable expansion, because this is good practice but if it contains no spaces this is not really necessary.

It was not, strictly speaking, necessary for me to add -r. Plain old sed will work if you say \([0-9]\{0,\}\), but the idea here was to simplify.


sed -i -e 's%<span id="unlockedCount">([0-9]*)</span\>/'"${unlockedCount}/g" index.html 

I removed the Useless Use of Cat, took out a bunch of unnecessary backslashes, added single quotes around the regex to protect it from shell expansion, and fixed the repetition operator. You might still need to backslash the grouping parentheses; my sed, at least, wants \(...\).

Note the use of single and double quotes next to each other. Single quotes protect against shell expansion, so you can't use them around "${unlockedCount}" where you do want the shell to interpolate the variable.