sed 's#&*;##g' <file>
does not do what you think it does. The
* character is a multiplier that says that the preceding character is repeated 0 or more times. The previous character is
&, so this would match e.g.
& is written 0 times before
;! This is what is matching in your test cases) but not what you want in this case.
You need to specify "any character" before the multiplier, which is represented by a single dot,
$ echo 'Text:3' | sed 's#&.*;##g'
That was the first problem. The second is the concept of so called "greedy" matching:
sed will see the first
& and then try to match the largest string it can. If you have several HTML entities on a single line, this would be a problem since:
$ echo 'Text:3 and some more text å and end' | sed 's#&.*;##g'
Text and end
If you want to see a fix in the
sed context, you could look for the ending character of the entity by matching any number of "not
;" before a closing
; by doing:
$ echo 'Text:3 and some more text å and end' | sed 's#&[^;]*;##g'
Text3 and some more text and end
You will still have problems with legitimate uses of the ampersand sign (
&) in the text (well,
& is the real "legitimate" use, but the real world is not always as parsable as the ideal one) and matching too much, but this explains why
sed is behaving the way it does.