How to recursively replace characters with sed?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












Is it possible to replace occurrences of a character sequence recursively without iterating over the same sequence again?



By performing a sed as in the following scenarios I can get the mentioned output.



echo XX | sed -e 's/XX/XoX/g' # outputs XoX
echo XXX | sed -e 's/XX/XoX/g' # outputs XoXX
echo XXXX | sed -e 's/XX/XoX/g' # outputs XoXXoX



However, I'm expecting the output to follow the following behavior.
XX -> XoX
XXX -> XoXoX
XXXX -> XoXoXoX



Is it possible to achieve the expected behavior with sed alone?










share|improve this question







New contributor




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























    up vote
    1
    down vote

    favorite












    Is it possible to replace occurrences of a character sequence recursively without iterating over the same sequence again?



    By performing a sed as in the following scenarios I can get the mentioned output.



    echo XX | sed -e 's/XX/XoX/g' # outputs XoX
    echo XXX | sed -e 's/XX/XoX/g' # outputs XoXX
    echo XXXX | sed -e 's/XX/XoX/g' # outputs XoXXoX



    However, I'm expecting the output to follow the following behavior.
    XX -> XoX
    XXX -> XoXoX
    XXXX -> XoXoXoX



    Is it possible to achieve the expected behavior with sed alone?










    share|improve this question







    New contributor




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





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Is it possible to replace occurrences of a character sequence recursively without iterating over the same sequence again?



      By performing a sed as in the following scenarios I can get the mentioned output.



      echo XX | sed -e 's/XX/XoX/g' # outputs XoX
      echo XXX | sed -e 's/XX/XoX/g' # outputs XoXX
      echo XXXX | sed -e 's/XX/XoX/g' # outputs XoXXoX



      However, I'm expecting the output to follow the following behavior.
      XX -> XoX
      XXX -> XoXoX
      XXXX -> XoXoXoX



      Is it possible to achieve the expected behavior with sed alone?










      share|improve this question







      New contributor




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











      Is it possible to replace occurrences of a character sequence recursively without iterating over the same sequence again?



      By performing a sed as in the following scenarios I can get the mentioned output.



      echo XX | sed -e 's/XX/XoX/g' # outputs XoX
      echo XXX | sed -e 's/XX/XoX/g' # outputs XoXX
      echo XXXX | sed -e 's/XX/XoX/g' # outputs XoXXoX



      However, I'm expecting the output to follow the following behavior.
      XX -> XoX
      XXX -> XoXoX
      XXXX -> XoXoXoX



      Is it possible to achieve the expected behavior with sed alone?







      linux bash regex sed






      share|improve this question







      New contributor




      Ishan Madhusanka 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 question







      New contributor




      Ishan Madhusanka 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 question




      share|improve this question






      New contributor




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









      asked 58 mins ago









      Ishan Madhusanka

      1084




      1084




      New contributor




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





      New contributor





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






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




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You can do:



          > echo XXXX | sed -e ':loop' -e 's/XX/XoX/g' -e 't loop'
          XoXoXoX


          With:




          • -e ':loop' : Create a "loop" label


          • -e 't loop' : Jump to the "loop" label if previous substitution was successful





          share|improve this answer



























            up vote
            1
            down vote













            In this particular case look-ahead or look-behind would be useful. I think GNU sed doesn't support these. With pearl:



            perl -ne 's/X(?=X)/Xo/g; print;'




            share



























              up vote
              0
              down vote













              I checked if there is any sort of a flag to make this happen.

              Even if that behavior was there it is going to be highly resource consuming.



              However, in this particular use case, it is possible to have the expression just twice and achieve the required functionality. i.e. with 2 repeating sed expressions.



              echo XX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoX
              echo XXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoX
              echo XXXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoXoX






              share|improve this answer








              New contributor




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

















                Your Answer







                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "3"
                ;
                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',
                convertImagesToLinks: true,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );






                Ishan Madhusanka is a new contributor. Be nice, and check out our Code of Conduct.









                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1366825%2fhow-to-recursively-replace-characters-with-sed%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                2
                down vote



                accepted










                You can do:



                > echo XXXX | sed -e ':loop' -e 's/XX/XoX/g' -e 't loop'
                XoXoXoX


                With:




                • -e ':loop' : Create a "loop" label


                • -e 't loop' : Jump to the "loop" label if previous substitution was successful





                share|improve this answer
























                  up vote
                  2
                  down vote



                  accepted










                  You can do:



                  > echo XXXX | sed -e ':loop' -e 's/XX/XoX/g' -e 't loop'
                  XoXoXoX


                  With:




                  • -e ':loop' : Create a "loop" label


                  • -e 't loop' : Jump to the "loop" label if previous substitution was successful





                  share|improve this answer






















                    up vote
                    2
                    down vote



                    accepted







                    up vote
                    2
                    down vote



                    accepted






                    You can do:



                    > echo XXXX | sed -e ':loop' -e 's/XX/XoX/g' -e 't loop'
                    XoXoXoX


                    With:




                    • -e ':loop' : Create a "loop" label


                    • -e 't loop' : Jump to the "loop" label if previous substitution was successful





                    share|improve this answer












                    You can do:



                    > echo XXXX | sed -e ':loop' -e 's/XX/XoX/g' -e 't loop'
                    XoXoXoX


                    With:




                    • -e ':loop' : Create a "loop" label


                    • -e 't loop' : Jump to the "loop" label if previous substitution was successful






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 32 mins ago









                    Gohu

                    447127




                    447127






















                        up vote
                        1
                        down vote













                        In this particular case look-ahead or look-behind would be useful. I think GNU sed doesn't support these. With pearl:



                        perl -ne 's/X(?=X)/Xo/g; print;'




                        share
























                          up vote
                          1
                          down vote













                          In this particular case look-ahead or look-behind would be useful. I think GNU sed doesn't support these. With pearl:



                          perl -ne 's/X(?=X)/Xo/g; print;'




                          share






















                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            In this particular case look-ahead or look-behind would be useful. I think GNU sed doesn't support these. With pearl:



                            perl -ne 's/X(?=X)/Xo/g; print;'




                            share












                            In this particular case look-ahead or look-behind would be useful. I think GNU sed doesn't support these. With pearl:



                            perl -ne 's/X(?=X)/Xo/g; print;'





                            share











                            share


                            share










                            answered 9 mins ago









                            Kamil Maciorowski

                            21.4k155071




                            21.4k155071




















                                up vote
                                0
                                down vote













                                I checked if there is any sort of a flag to make this happen.

                                Even if that behavior was there it is going to be highly resource consuming.



                                However, in this particular use case, it is possible to have the expression just twice and achieve the required functionality. i.e. with 2 repeating sed expressions.



                                echo XX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoX
                                echo XXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoX
                                echo XXXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoXoX






                                share|improve this answer








                                New contributor




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





















                                  up vote
                                  0
                                  down vote













                                  I checked if there is any sort of a flag to make this happen.

                                  Even if that behavior was there it is going to be highly resource consuming.



                                  However, in this particular use case, it is possible to have the expression just twice and achieve the required functionality. i.e. with 2 repeating sed expressions.



                                  echo XX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoX
                                  echo XXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoX
                                  echo XXXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoXoX






                                  share|improve this answer








                                  New contributor




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



















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    I checked if there is any sort of a flag to make this happen.

                                    Even if that behavior was there it is going to be highly resource consuming.



                                    However, in this particular use case, it is possible to have the expression just twice and achieve the required functionality. i.e. with 2 repeating sed expressions.



                                    echo XX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoX
                                    echo XXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoX
                                    echo XXXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoXoX






                                    share|improve this answer








                                    New contributor




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









                                    I checked if there is any sort of a flag to make this happen.

                                    Even if that behavior was there it is going to be highly resource consuming.



                                    However, in this particular use case, it is possible to have the expression just twice and achieve the required functionality. i.e. with 2 repeating sed expressions.



                                    echo XX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoX
                                    echo XXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoX
                                    echo XXXX | sed -e 's/XX/XoX/g' -e 's/XX/XoX/g' # outputs XoXoXoX







                                    share|improve this answer








                                    New contributor




                                    Ishan Madhusanka 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




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









                                    answered 31 mins ago









                                    Ishan Madhusanka

                                    1084




                                    1084




                                    New contributor




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





                                    New contributor





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






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




















                                        Ishan Madhusanka is a new contributor. Be nice, and check out our Code of Conduct.









                                         

                                        draft saved


                                        draft discarded


















                                        Ishan Madhusanka is a new contributor. Be nice, and check out our Code of Conduct.












                                        Ishan Madhusanka is a new contributor. Be nice, and check out our Code of Conduct.











                                        Ishan Madhusanka is a new contributor. Be nice, and check out our Code of Conduct.













                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1366825%2fhow-to-recursively-replace-characters-with-sed%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Comments

                                        Popular posts from this blog

                                        What does second last employer means? [closed]

                                        List of Gilmore Girls characters

                                        Confectionery