Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove specific tag with its contents using sed

I would like to remove following tag from HTML including its constantly varying contents:

<span class="the_class_name">li4tuq734g23r74r7Whatever</span>

A following BASH script

.... | sed -e :a -re 's/<span class="the_class_name"/>.*</span>//g' > "$NewFile"

ends with error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

sed: -e expression #2, char XX: unknown option to `s’

I tried to escape quotes, slashes and "less than" symbols in various combinations and still get this error.

>Solution :

I suggest using a different separator than / when / is contained within the thing you want to match on. Also, prefer -E instead of -r for extended regex to be Posix compatible. Also note that you have a / in your first span in your regex that doesn’t belong there.
Also, .* will make it overly greedy and eat up any </span> that follows the first </span> on the line. It’s better to match on [^<]*. That is, any character that is not <.

sed -E 's,<span class="the_class_name">[^<]*</span>,,g'

A better option is of course to use a HTML parser for this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading