sed search and replace with variable containing slash

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








up vote
6
down vote

favorite












I am trying to replace a string inside a text file with a variable.



I am using this code:



sed -i "s/search text /TAG//replace text $variable/" $textfile


The content of the variable is /newTAG/. And the content of the variable is the problem, more accuratelyte the / at the beginning and the end.



When both / are removed so the content is only newTAG everything works fine.



I already tried the following



sed -i "s/search text /TAG//replace text "$variable"/" $textfile
sed -i 's/search text /TAG//replace text "$variable"/' $textfile


Can anybody help me?
Thanks










share|improve this question



























    up vote
    6
    down vote

    favorite












    I am trying to replace a string inside a text file with a variable.



    I am using this code:



    sed -i "s/search text /TAG//replace text $variable/" $textfile


    The content of the variable is /newTAG/. And the content of the variable is the problem, more accuratelyte the / at the beginning and the end.



    When both / are removed so the content is only newTAG everything works fine.



    I already tried the following



    sed -i "s/search text /TAG//replace text "$variable"/" $textfile
    sed -i 's/search text /TAG//replace text "$variable"/' $textfile


    Can anybody help me?
    Thanks










    share|improve this question

























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      I am trying to replace a string inside a text file with a variable.



      I am using this code:



      sed -i "s/search text /TAG//replace text $variable/" $textfile


      The content of the variable is /newTAG/. And the content of the variable is the problem, more accuratelyte the / at the beginning and the end.



      When both / are removed so the content is only newTAG everything works fine.



      I already tried the following



      sed -i "s/search text /TAG//replace text "$variable"/" $textfile
      sed -i 's/search text /TAG//replace text "$variable"/' $textfile


      Can anybody help me?
      Thanks










      share|improve this question















      I am trying to replace a string inside a text file with a variable.



      I am using this code:



      sed -i "s/search text /TAG//replace text $variable/" $textfile


      The content of the variable is /newTAG/. And the content of the variable is the problem, more accuratelyte the / at the beginning and the end.



      When both / are removed so the content is only newTAG everything works fine.



      I already tried the following



      sed -i "s/search text /TAG//replace text "$variable"/" $textfile
      sed -i 's/search text /TAG//replace text "$variable"/' $textfile


      Can anybody help me?
      Thanks







      command-line bash sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 10 '17 at 17:49









      muru

      130k19275470




      130k19275470










      asked May 10 '17 at 9:25









      Bird Person

      332




      332




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          9
          down vote



          accepted










          Use something other than / as the delimiter for the regex (for example, the colon :):



          sed -i "s:search text /TAG/:replace text $variable:" "$textfile"


          Or escape the / in the variable.






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "89"
            ;
            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
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f913688%2fsed-search-and-replace-with-variable-containing-slash%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            9
            down vote



            accepted










            Use something other than / as the delimiter for the regex (for example, the colon :):



            sed -i "s:search text /TAG/:replace text $variable:" "$textfile"


            Or escape the / in the variable.






            share|improve this answer
























              up vote
              9
              down vote



              accepted










              Use something other than / as the delimiter for the regex (for example, the colon :):



              sed -i "s:search text /TAG/:replace text $variable:" "$textfile"


              Or escape the / in the variable.






              share|improve this answer






















                up vote
                9
                down vote



                accepted







                up vote
                9
                down vote



                accepted






                Use something other than / as the delimiter for the regex (for example, the colon :):



                sed -i "s:search text /TAG/:replace text $variable:" "$textfile"


                Or escape the / in the variable.






                share|improve this answer












                Use something other than / as the delimiter for the regex (for example, the colon :):



                sed -i "s:search text /TAG/:replace text $variable:" "$textfile"


                Or escape the / in the variable.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 10 '17 at 9:36









                muru

                130k19275470




                130k19275470



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f913688%2fsed-search-and-replace-with-variable-containing-slash%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    pylint3 and pip3 broken

                    Missing snmpget and snmpwalk

                    How to enroll fingerprints to Ubuntu 17.10 with VFS491