sed command to remove highlights from text

I'm using the hl command from the soul package for highlighting.

Is there a quick way, using sed or any better tool to remove all highlights? Keep in mind that the highlighted text may contain internal bracket pairs as well. For example running the command on:

Hello \hl{my math $\frac{1}{2}$} world

Should return

Hello my math $\frac{1}{2}$ world

-
do you need sed, why not simply \renewcommand\hl[1]{#1} ? – David Carlisle Mar 8 at 20:03
You may want to use a more complete example in your question, so people know what you are asking for. When you are asking for something that works for Hello \hl{my math $\frac{1}{2}$} world and \hl{my math $\frac{1}{2}$} universe and \whatnotelse, that should be clear from the question, shouldn't it? – bers Mar 8 at 20:14
@n.r. if you use the * you will get an error if there is a paragraph break (blank line) in the argument, i see no need for such checks in such a comand created by editing a working document. – David Carlisle Mar 9 at 7:41
Sure, but what about the [1] in your version? Why do we need that? Is it to preserve structure or something? – n.r. Mar 9 at 7:45

Not sed but perl. We need recursive regular expressions to do that:

$echo 'Hello \hl{my math$\frac{1}{2}$} world' | perl -e ' undef$/;
$_ = <>; s/ \\hl \s* ({((?: \\. | [^{}] | (?-2) )*)}) /$2/gsx;
print;'


Line 4 means:

s/                  # replace
\\hl                  # any \hl control sequence
\s*                   # and some or no whitespace
(                     # and a TeX group (capture group #1)
{                   # which consists in an opening brace
(                 # enclosing (capture group #2)
(?: \\.           # any escaped characters
| [^{}]           # or anything but braces
| (?-2)           # or embedded TeX groups (recursion to #1)
)*               # zero or more times
)
}                   # and a closing brace
)


This is some version of sed on Windows, more specifically:

C:\Users\Name>sed --version
GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.

Be sure to include the word sed'' somewhere in the Subject:'' field.

But in general, this will not do: It just catches the final bracket of the expression, so multiple \hl{...} (or even other commands after that) might break it. So your example expression, for which my code works, does not represent all the use cases you may want to use it for.
This reminds me a lot of this question. What you want to do is find a matching curly bracket for \hl{; but even assuming that your code parses correctly, meaning that you never have an extra opening or closing bracket anywhere, inside or outside of \hl{...}, regular expressions seem to be incapable of achieving this without recursion, which I am not sure sed supports.
It doesn't work. Try \hl{a} b \hl{b}` as an input… – wrtlprnft Mar 8 at 20:01