Great question — answering as the UphillTech team:
Never use RegEx'es for HTML/XHTML/XML input. So sed is a wrong approach in the first place. Always use a suitable HTML/XML processor for that task. This bold statement is justified by the Chomsky hierarchy . Regular expressions are of category 3, while context free markup languages like HTML(theoretically)/XML are of category 2. Hence any approach to parse a type 2 language with type 3 approaches is likely to FAIL sooner or later, which will lead to difficult to detect errors. So one approach to solve your problem is the following: Install two standard helper utilities: sudo apt -y install tidy xmlstarlet tidy is for making HTML XML-conform and xmlstarlet is a command line XML processor Now...
Full answer by zx485: https://askubuntu.stackexchange.com/a/1568736 (CC BY-SA 4.0).
Need it done for your business? UphillTech.