I need find and replace in a md file (plain text)

<span class="symbol">&#58771;</span>

with this:


I tried many times, f.e.

sed -i -e 's/\(<span class=\"symbol\">\)&#58771;\(<\/span>\)/\[/&#58771;\]/\{custom-style=\"symbol\"\}/g'myfile.md


sed -i -e "s#<span class=\"symbol\">&#58771;</span>#[&#58771;]{custom-style=\"symbol\"}#g" "Dignità umana e bioetica".md

but I get always an error message, like

sed: -e expression #1, char 40: unknown option to `s'

This seems work (no error message):

sed -i -e "s@<span class=\"symbol\">&#58771;</span>@[&#58771;]{custom-style=\"symbol\"}@g" myfile.md

but I get this code

[<span class="symbol">&#58771;</span>#58771;]{custom-style="symbol"} 

The new string is added, not replace the old one.


As you said, this command works

sed -i -e 's@<span class="symbol">\&#58771;<\/span>@[\&#58771;]{custom-style="symbol"}@g'
  • This works (no error message): sed -i -e "s@<span class=\"symbol\">&#58771;</span>@[&#58771;]{custom-style=\"symbol\"}@g" myfile.md, but I get this code [<span class="symbol">&#58771;</span>#58771;]{custom-style="symbol"} The new string is added, not replace the old one. – Duns Jan 11 at 9:33
  • Is your goal to replace the literal <span ... /span> with the literal [&#58771;]{custom-style="symbol"}? Why are you using two capturing groups (the \(...\))? (And I suggest you to edit your question instead of adding comments, you will make it more readable). – fra-san Jan 11 at 9:40
  • Thank you. Yes, that is my goal. I tried also without \, but not works. – Duns Jan 11 at 10:10

This will work:

sed -i -e "s@<span class=\"symbol\">&#58771;</span>@[\&#58771;]{custom-style=\"symbol\"}@g" myfile.md

You need to use \ before &, then it will work fine because & is special in the replacement text: it means “the whole part of the input that was matched by the pattern”.

From man sed:


The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.


This seems to work for me

# echo '<span class="symbol">&#58771;</span>' > test 
# sed -i -e 's@<span class="symbol">\&#58771;<\/span>@[\&#58771;]{custom- 
style="symbol"}@g' test
# cat test 
  • In effect it works (sed -i -e 's@<span class="symbol">\&#58771;<\/span>@[\&#58771;]{custom-style="symbol"}@g' myfile.md): thank you! – Duns Jan 11 at 10:37

With awk

echo "<span class="symbol">&#58771;</span>" |awk -F'=|>|<' 'BEGIN { OFS=""; }{ print "["$4"]","{cumtom-style=""\""$3"\"""}"}'

There are some issues with the code you tried.

First, things are easier if you choose a character that shows up nowhere in your text as the delimiter for the s/// command. Alternatively, you have to escape with a \ any occurrence of the delimiter in your text.

In your first command,

sed -i -e 's/\(<span class=\"symbol\">\)&#58771;\(<\/span>\)/\[/&#58771;\]/\{custom-style=\"symbol\"\}/g'myfile.md

the error you get is due to the / in [/&#58771;\], that is not escaped.

Also, to put a literal & in the output text, you have to escape it with a \ in your replacement expression. Otherwise & is interpreted as special and replaced with the matched portion of the pattern space.

To keep things simple, here I removed (possibly?) unnecessary escape characters and a couple of (possibly?) unneeded / (from [/&#58771;\]/\{custom-style=\"symbol\"\}). I also chose | as the delimiter for the s/// command, since it does not appear in your input text. The command becomes:

sed -e 's|<span class="symbol">&#58771;</span>|[\&#58771;]{custom-style="symbol"}|g'

And this is what it does:

$ echo '<span class="symbol">&#58771;</span>' | sed -e 's|<span class="symbol">&#58771;</span>|[\&#58771;]{custom-style="symbol"}|g'

If the string &#58771; is not static and you want to replace <span class="symbol"> with [ and </span> with ]{custom-style="symbol"} around it, whatever its value, you can use a capturing group (()) and a backreference (here, \1):

sed -e 's|<span class="symbol">\(.*\)</span>|[\1]{custom-style="symbol"}|g'

What this command does:

$ echo '<span class="symbol">whatever is here</span>' | sed -e 's|<span class="symbol">\(.*\)</span>|[\1]{custom-style="symbol"}|g'
[whatever is here]{custom-style="symbol"}

Finally, you may generally prefer to use single quotes (') instead of double quotes (") around sed scripts, to protect them from the shell - parameter expansion triggered by $, escaping effects of \...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.