find text but replace a text beside it

I have an html file that looks like this (this is just a part of the html file):

<td colspan="3" rowspan="1" style="text-align: center; background-color: rgb(<!-- IDENTIFIER1 -->51, 255, 51);"><small><!-- IDENTIFIER2 -->UP</small></td>

This is to automatically update the status of the environment:

  1. Find the IDENTIFIER1.
  2. Replace 51, 255, 51 with some number (e.g, 0, 0, 0)
  3. Find IDENTIFIER 2.
  4. Replace UP or DOWN with a some value (e.g, UP or DOWN as well)

I know i can us a simple find and replace substitution using SED but it will
be a very long line of code and can be mixed up with all the characters on the line so I though of using the above logic but i dont know how to do it. Appreciate your help.

I'm not sure if you are saying you are already using this, and would like to avoid it, but then I don't really see what would be much simpler than this.

sed -e 's/\(<!-- IDENTIFIER1 -->\)[0-9]*,[0-9]*,[0-9]*/\10,0,0/' \
  -e 's/\(<!-- IDENTIFIER2 -->\)UP/\1DOWN/' old.html >new.html

If the replacement values should not be hardcoded, you need to adjust the quoting; single quotes prevent variable expansion, but you can mix single and double quotes freely to protect the parts which need protection and still get variable interpolation where you want it.

Thanks a lot! That saves time.

That works for the character substitution but didnt work for the number substitutions:
sed -e 's/\(<!-- IDENTIFIER1 -->\)[0-9]*,[0-9]*,[0-9]*/\10,0,0/'

Looks like you need to add spaces after the commas, sorry for missing that.

yep :), the space was missed.. thanks for the help :slight_smile: