sed find and replace html tag












1















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



<span class="symbol"></span>


with this:



[]{custom-style="symbol"}


I tried many times, f.e.



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


or



sed -i -e "s#<span class="symbol"></span>#[]{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"></span>@[]{custom-style="symbol"}@g" myfile.md


but I get this code



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


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



solved



As you said, this command works



sed -i -e 's@<span class="symbol"></span>@[]{custom-style="symbol"}@g'









share|improve this question

























  • This works (no error message): sed -i -e "s@<span class="symbol"></span>@[]{custom-style="symbol"}@g" myfile.md, but I get this code [<span class="symbol"></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 []{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











  • You may want to accept the answer that you feel more fitting, since that is the standard way to mark a issue as solved on SE sites.

    – fra-san
    Jan 11 at 11:27
















1















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



<span class="symbol"></span>


with this:



[]{custom-style="symbol"}


I tried many times, f.e.



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


or



sed -i -e "s#<span class="symbol"></span>#[]{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"></span>@[]{custom-style="symbol"}@g" myfile.md


but I get this code



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


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



solved



As you said, this command works



sed -i -e 's@<span class="symbol"></span>@[]{custom-style="symbol"}@g'









share|improve this question

























  • This works (no error message): sed -i -e "s@<span class="symbol"></span>@[]{custom-style="symbol"}@g" myfile.md, but I get this code [<span class="symbol"></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 []{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











  • You may want to accept the answer that you feel more fitting, since that is the standard way to mark a issue as solved on SE sites.

    – fra-san
    Jan 11 at 11:27














1












1








1








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



<span class="symbol"></span>


with this:



[]{custom-style="symbol"}


I tried many times, f.e.



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


or



sed -i -e "s#<span class="symbol"></span>#[]{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"></span>@[]{custom-style="symbol"}@g" myfile.md


but I get this code



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


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



solved



As you said, this command works



sed -i -e 's@<span class="symbol"></span>@[]{custom-style="symbol"}@g'









share|improve this question
















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



<span class="symbol"></span>


with this:



[]{custom-style="symbol"}


I tried many times, f.e.



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


or



sed -i -e "s#<span class="symbol"></span>#[]{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"></span>@[]{custom-style="symbol"}@g" myfile.md


but I get this code



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


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



solved



As you said, this command works



sed -i -e 's@<span class="symbol"></span>@[]{custom-style="symbol"}@g'






sed find replace html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 12 at 20:34









Rui F Ribeiro

39.5k1479132




39.5k1479132










asked Jan 11 at 9:08









DunsDuns

195




195













  • This works (no error message): sed -i -e "s@<span class="symbol"></span>@[]{custom-style="symbol"}@g" myfile.md, but I get this code [<span class="symbol"></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 []{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











  • You may want to accept the answer that you feel more fitting, since that is the standard way to mark a issue as solved on SE sites.

    – fra-san
    Jan 11 at 11:27



















  • This works (no error message): sed -i -e "s@<span class="symbol"></span>@[]{custom-style="symbol"}@g" myfile.md, but I get this code [<span class="symbol"></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 []{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











  • You may want to accept the answer that you feel more fitting, since that is the standard way to mark a issue as solved on SE sites.

    – fra-san
    Jan 11 at 11:27

















This works (no error message): sed -i -e "s@<span class="symbol"></span>@[]{custom-style="symbol"}@g" myfile.md, but I get this code [<span class="symbol"></span>#58771;]{custom-style="symbol"} The new string is added, not replace the old one.

– Duns
Jan 11 at 9:33







This works (no error message): sed -i -e "s@<span class="symbol"></span>@[]{custom-style="symbol"}@g" myfile.md, but I get this code [<span class="symbol"></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 []{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





Is your goal to replace the literal <span ... /span> with the literal []{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





Thank you. Yes, that is my goal. I tried also without , but not works.

– Duns
Jan 11 at 10:10













You may want to accept the answer that you feel more fitting, since that is the standard way to mark a issue as solved on SE sites.

– fra-san
Jan 11 at 11:27





You may want to accept the answer that you feel more fitting, since that is the standard way to mark a issue as solved on SE sites.

– fra-san
Jan 11 at 11:27










4 Answers
4






active

oldest

votes


















1














This will work:



sed -i -e "s@<span class="symbol"></span>@[]{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:




s/regexp/replacement/



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.







share|improve this answer

































    0














    This seems to work for me



    # echo '<span class="symbol"></span>' > test 
    # sed -i -e 's@<span class="symbol"></span>@[]{custom-
    style="symbol"}@g' test
    # cat test
    []{custom-style="symbol"}





    share|improve this answer








    New contributor




    Timur.C is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















    • In effect it works (sed -i -e 's@<span class="symbol"></span>@[]{custom-style="symbol"}@g' myfile.md): thank you!

      – Duns
      Jan 11 at 10:37



















    0














    With awk



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





    share|improve this answer































      0
















      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">)(</span>)/[/]/{custom-style="symbol"}/g'myfile.md


      the error you get is due to the / in [/], 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 [/]/{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"></span>|[]{custom-style="symbol"}|g'


      And this is what it does:



      $ echo '<span class="symbol"></span>' | sed -e 's|<span class="symbol"></span>|[]{custom-style="symbol"}|g'
      []{custom-style="symbol"}


      If the string 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 ...






      share|improve this answer

























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "106"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493901%2fsed-find-and-replace-html-tag%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        This will work:



        sed -i -e "s@<span class="symbol"></span>@[]{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:




        s/regexp/replacement/



        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.







        share|improve this answer






























          1














          This will work:



          sed -i -e "s@<span class="symbol"></span>@[]{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:




          s/regexp/replacement/



          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.







          share|improve this answer




























            1












            1








            1







            This will work:



            sed -i -e "s@<span class="symbol"></span>@[]{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:




            s/regexp/replacement/



            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.







            share|improve this answer















            This will work:



            sed -i -e "s@<span class="symbol"></span>@[]{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:




            s/regexp/replacement/



            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.








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 11 at 14:46

























            answered Jan 11 at 10:32









            P_YadavP_Yadav

            1,6153924




            1,6153924

























                0














                This seems to work for me



                # echo '<span class="symbol"></span>' > test 
                # sed -i -e 's@<span class="symbol"></span>@[]{custom-
                style="symbol"}@g' test
                # cat test
                []{custom-style="symbol"}





                share|improve this answer








                New contributor




                Timur.C is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





















                • In effect it works (sed -i -e 's@<span class="symbol"></span>@[]{custom-style="symbol"}@g' myfile.md): thank you!

                  – Duns
                  Jan 11 at 10:37
















                0














                This seems to work for me



                # echo '<span class="symbol"></span>' > test 
                # sed -i -e 's@<span class="symbol"></span>@[]{custom-
                style="symbol"}@g' test
                # cat test
                []{custom-style="symbol"}





                share|improve this answer








                New contributor




                Timur.C is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





















                • In effect it works (sed -i -e 's@<span class="symbol"></span>@[]{custom-style="symbol"}@g' myfile.md): thank you!

                  – Duns
                  Jan 11 at 10:37














                0












                0








                0







                This seems to work for me



                # echo '<span class="symbol"></span>' > test 
                # sed -i -e 's@<span class="symbol"></span>@[]{custom-
                style="symbol"}@g' test
                # cat test
                []{custom-style="symbol"}





                share|improve this answer








                New contributor




                Timur.C is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.










                This seems to work for me



                # echo '<span class="symbol"></span>' > test 
                # sed -i -e 's@<span class="symbol"></span>@[]{custom-
                style="symbol"}@g' test
                # cat test
                []{custom-style="symbol"}






                share|improve this answer








                New contributor




                Timur.C is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer






                New contributor




                Timur.C is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered Jan 11 at 10:31









                Timur.CTimur.C

                1




                1




                New contributor




                Timur.C is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                Timur.C is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                Timur.C is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.













                • In effect it works (sed -i -e 's@<span class="symbol"></span>@[]{custom-style="symbol"}@g' myfile.md): thank you!

                  – Duns
                  Jan 11 at 10:37



















                • In effect it works (sed -i -e 's@<span class="symbol"></span>@[]{custom-style="symbol"}@g' myfile.md): thank you!

                  – Duns
                  Jan 11 at 10:37

















                In effect it works (sed -i -e 's@<span class="symbol"></span>@[]{custom-style="symbol"}@g' myfile.md): thank you!

                – Duns
                Jan 11 at 10:37





                In effect it works (sed -i -e 's@<span class="symbol"></span>@[]{custom-style="symbol"}@g' myfile.md): thank you!

                – Duns
                Jan 11 at 10:37











                0














                With awk



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





                share|improve this answer




























                  0














                  With awk



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





                  share|improve this answer


























                    0












                    0








                    0







                    With awk



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





                    share|improve this answer













                    With awk



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






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 11 at 11:16









                    Emilio GalarragaEmilio Galarraga

                    50929




                    50929























                        0
















                        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">)(</span>)/[/]/{custom-style="symbol"}/g'myfile.md


                        the error you get is due to the / in [/], 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 [/]/{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"></span>|[]{custom-style="symbol"}|g'


                        And this is what it does:



                        $ echo '<span class="symbol"></span>' | sed -e 's|<span class="symbol"></span>|[]{custom-style="symbol"}|g'
                        []{custom-style="symbol"}


                        If the string 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 ...






                        share|improve this answer






























                          0
















                          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">)(</span>)/[/]/{custom-style="symbol"}/g'myfile.md


                          the error you get is due to the / in [/], 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 [/]/{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"></span>|[]{custom-style="symbol"}|g'


                          And this is what it does:



                          $ echo '<span class="symbol"></span>' | sed -e 's|<span class="symbol"></span>|[]{custom-style="symbol"}|g'
                          []{custom-style="symbol"}


                          If the string 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 ...






                          share|improve this answer




























                            0












                            0








                            0









                            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">)(</span>)/[/]/{custom-style="symbol"}/g'myfile.md


                            the error you get is due to the / in [/], 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 [/]/{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"></span>|[]{custom-style="symbol"}|g'


                            And this is what it does:



                            $ echo '<span class="symbol"></span>' | sed -e 's|<span class="symbol"></span>|[]{custom-style="symbol"}|g'
                            []{custom-style="symbol"}


                            If the string 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 ...






                            share|improve this answer

















                            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">)(</span>)/[/]/{custom-style="symbol"}/g'myfile.md


                            the error you get is due to the / in [/], 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 [/]/{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"></span>|[]{custom-style="symbol"}|g'


                            And this is what it does:



                            $ echo '<span class="symbol"></span>' | sed -e 's|<span class="symbol"></span>|[]{custom-style="symbol"}|g'
                            []{custom-style="symbol"}


                            If the string 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 ...







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 11 at 21:13

























                            answered Jan 11 at 10:35









                            fra-sanfra-san

                            1,267214




                            1,267214






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493901%2fsed-find-and-replace-html-tag%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to make a Squid Proxy server?

                                Is this a new Fibonacci Identity?

                                19世紀