Replace text with another

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











up vote
-1
down vote

favorite












I would like to replace a string with another string. For example:
I would like to change the following text:



"listen_addresses = ['127.0.0.1:53', '[::1]:53']"


to the following text



"listen_addresses = ['127.0.0.1:40', '[::1]:40']"


I tried this command:



sudo sed "s#listen_addresses = ['127.0.0.1:53', '[::1]:53']'#listen_addresses = ['127.0.0.1:40', '[::1]:40']#g"


and I got this result:



input: listen_addresses = ['127.0.0.1:53', '[::1]:53']



output: listen_addresses = ['127.0.0.1:53', '[::1]:53']



What am I doing wrong?










share|improve this question























  • you only need to escape [ and ], and in first part of replacement.
    – Archemar
    Sep 3 at 12:17







  • 3




    You're not "replacing any string", you're replacing a very specific string -- the port at the end of a listen_address configuration list in a file. Understanding exactly what you want leads people a long way towards the correct answer.
    – Jeff Schaller
    Sep 3 at 12:20














up vote
-1
down vote

favorite












I would like to replace a string with another string. For example:
I would like to change the following text:



"listen_addresses = ['127.0.0.1:53', '[::1]:53']"


to the following text



"listen_addresses = ['127.0.0.1:40', '[::1]:40']"


I tried this command:



sudo sed "s#listen_addresses = ['127.0.0.1:53', '[::1]:53']'#listen_addresses = ['127.0.0.1:40', '[::1]:40']#g"


and I got this result:



input: listen_addresses = ['127.0.0.1:53', '[::1]:53']



output: listen_addresses = ['127.0.0.1:53', '[::1]:53']



What am I doing wrong?










share|improve this question























  • you only need to escape [ and ], and in first part of replacement.
    – Archemar
    Sep 3 at 12:17







  • 3




    You're not "replacing any string", you're replacing a very specific string -- the port at the end of a listen_address configuration list in a file. Understanding exactly what you want leads people a long way towards the correct answer.
    – Jeff Schaller
    Sep 3 at 12:20












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I would like to replace a string with another string. For example:
I would like to change the following text:



"listen_addresses = ['127.0.0.1:53', '[::1]:53']"


to the following text



"listen_addresses = ['127.0.0.1:40', '[::1]:40']"


I tried this command:



sudo sed "s#listen_addresses = ['127.0.0.1:53', '[::1]:53']'#listen_addresses = ['127.0.0.1:40', '[::1]:40']#g"


and I got this result:



input: listen_addresses = ['127.0.0.1:53', '[::1]:53']



output: listen_addresses = ['127.0.0.1:53', '[::1]:53']



What am I doing wrong?










share|improve this question















I would like to replace a string with another string. For example:
I would like to change the following text:



"listen_addresses = ['127.0.0.1:53', '[::1]:53']"


to the following text



"listen_addresses = ['127.0.0.1:40', '[::1]:40']"


I tried this command:



sudo sed "s#listen_addresses = ['127.0.0.1:53', '[::1]:53']'#listen_addresses = ['127.0.0.1:40', '[::1]:40']#g"


and I got this result:



input: listen_addresses = ['127.0.0.1:53', '[::1]:53']



output: listen_addresses = ['127.0.0.1:53', '[::1]:53']



What am I doing wrong?







sed regular-expression replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 10 at 5:47









DarkHeart

3,38822137




3,38822137










asked Sep 3 at 12:09









a.gulcan

113




113











  • you only need to escape [ and ], and in first part of replacement.
    – Archemar
    Sep 3 at 12:17







  • 3




    You're not "replacing any string", you're replacing a very specific string -- the port at the end of a listen_address configuration list in a file. Understanding exactly what you want leads people a long way towards the correct answer.
    – Jeff Schaller
    Sep 3 at 12:20
















  • you only need to escape [ and ], and in first part of replacement.
    – Archemar
    Sep 3 at 12:17







  • 3




    You're not "replacing any string", you're replacing a very specific string -- the port at the end of a listen_address configuration list in a file. Understanding exactly what you want leads people a long way towards the correct answer.
    – Jeff Schaller
    Sep 3 at 12:20















you only need to escape [ and ], and in first part of replacement.
– Archemar
Sep 3 at 12:17





you only need to escape [ and ], and in first part of replacement.
– Archemar
Sep 3 at 12:17





3




3




You're not "replacing any string", you're replacing a very specific string -- the port at the end of a listen_address configuration list in a file. Understanding exactly what you want leads people a long way towards the correct answer.
– Jeff Schaller
Sep 3 at 12:20




You're not "replacing any string", you're replacing a very specific string -- the port at the end of a listen_address configuration list in a file. Understanding exactly what you want leads people a long way towards the correct answer.
– Jeff Schaller
Sep 3 at 12:20










3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










as explain in comment, only [ and ] are to be escaped



echo "listen_addresses = ['127.0.0.1:53', '[::1]:53']" |
sed -e "s#listen_addresses = ['127.0.0.1:53', '[::1]:53'#listen_addresses = ['127.0.0.1:40', '[::1]:40'#"


which give



listen_addresses = ['127.0.0.1:40', '[::1]:40']





share|improve this answer




















  • Thank u for answer yes it's work
    – a.gulcan
    Sep 3 at 12:28

















up vote
0
down vote













You can do:



sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file



  • Searches for the exact string listen_addresses = ['127.0.0.1:53', '[::1]:53']

  • If the string matches s/53/40/g will replace all occurences of 53 with 40 (but only in the matched line)


Example output:



$ cat file
listen_addresses = ['127.0.0.1:53', '[::1]:53']
listen_addresses = ['127.0.0.2:53', '[::1]:53']

$ sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
listen_addresses = ['127.0.0.1:40', '[::1]:40']
listen_addresses = ['127.0.0.2:53', '[::1]:53']


As you can see, an exact match is needed to perform the replace.






share|improve this answer



























    up vote
    0
    down vote













    sed s/":53'"/":40'"/g <<<"listen_addresses = ['127.0.0.1:53', '[::1]:53']"


    do something close to you asked. But I agree with comment of @Jeff Schaller!



    From your question nobody can recognize why do you simply do not it by hand? What is the text from which you get your lines, how they differs each other? what variants should be covered by regex?



    sed is good for replacing but for electing the coresct line the grep is better.






    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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2funix.stackexchange.com%2fquestions%2f466562%2freplace-text-with-another%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
      1
      down vote



      accepted










      as explain in comment, only [ and ] are to be escaped



      echo "listen_addresses = ['127.0.0.1:53', '[::1]:53']" |
      sed -e "s#listen_addresses = ['127.0.0.1:53', '[::1]:53'#listen_addresses = ['127.0.0.1:40', '[::1]:40'#"


      which give



      listen_addresses = ['127.0.0.1:40', '[::1]:40']





      share|improve this answer




















      • Thank u for answer yes it's work
        – a.gulcan
        Sep 3 at 12:28














      up vote
      1
      down vote



      accepted










      as explain in comment, only [ and ] are to be escaped



      echo "listen_addresses = ['127.0.0.1:53', '[::1]:53']" |
      sed -e "s#listen_addresses = ['127.0.0.1:53', '[::1]:53'#listen_addresses = ['127.0.0.1:40', '[::1]:40'#"


      which give



      listen_addresses = ['127.0.0.1:40', '[::1]:40']





      share|improve this answer




















      • Thank u for answer yes it's work
        – a.gulcan
        Sep 3 at 12:28












      up vote
      1
      down vote



      accepted







      up vote
      1
      down vote



      accepted






      as explain in comment, only [ and ] are to be escaped



      echo "listen_addresses = ['127.0.0.1:53', '[::1]:53']" |
      sed -e "s#listen_addresses = ['127.0.0.1:53', '[::1]:53'#listen_addresses = ['127.0.0.1:40', '[::1]:40'#"


      which give



      listen_addresses = ['127.0.0.1:40', '[::1]:40']





      share|improve this answer












      as explain in comment, only [ and ] are to be escaped



      echo "listen_addresses = ['127.0.0.1:53', '[::1]:53']" |
      sed -e "s#listen_addresses = ['127.0.0.1:53', '[::1]:53'#listen_addresses = ['127.0.0.1:40', '[::1]:40'#"


      which give



      listen_addresses = ['127.0.0.1:40', '[::1]:40']






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Sep 3 at 12:25









      Archemar

      19.1k93467




      19.1k93467











      • Thank u for answer yes it's work
        – a.gulcan
        Sep 3 at 12:28
















      • Thank u for answer yes it's work
        – a.gulcan
        Sep 3 at 12:28















      Thank u for answer yes it's work
      – a.gulcan
      Sep 3 at 12:28




      Thank u for answer yes it's work
      – a.gulcan
      Sep 3 at 12:28












      up vote
      0
      down vote













      You can do:



      sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file



      • Searches for the exact string listen_addresses = ['127.0.0.1:53', '[::1]:53']

      • If the string matches s/53/40/g will replace all occurences of 53 with 40 (but only in the matched line)


      Example output:



      $ cat file
      listen_addresses = ['127.0.0.1:53', '[::1]:53']
      listen_addresses = ['127.0.0.2:53', '[::1]:53']

      $ sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
      listen_addresses = ['127.0.0.1:40', '[::1]:40']
      listen_addresses = ['127.0.0.2:53', '[::1]:53']


      As you can see, an exact match is needed to perform the replace.






      share|improve this answer
























        up vote
        0
        down vote













        You can do:



        sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file



        • Searches for the exact string listen_addresses = ['127.0.0.1:53', '[::1]:53']

        • If the string matches s/53/40/g will replace all occurences of 53 with 40 (but only in the matched line)


        Example output:



        $ cat file
        listen_addresses = ['127.0.0.1:53', '[::1]:53']
        listen_addresses = ['127.0.0.2:53', '[::1]:53']

        $ sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
        listen_addresses = ['127.0.0.1:40', '[::1]:40']
        listen_addresses = ['127.0.0.2:53', '[::1]:53']


        As you can see, an exact match is needed to perform the replace.






        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          You can do:



          sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file



          • Searches for the exact string listen_addresses = ['127.0.0.1:53', '[::1]:53']

          • If the string matches s/53/40/g will replace all occurences of 53 with 40 (but only in the matched line)


          Example output:



          $ cat file
          listen_addresses = ['127.0.0.1:53', '[::1]:53']
          listen_addresses = ['127.0.0.2:53', '[::1]:53']

          $ sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
          listen_addresses = ['127.0.0.1:40', '[::1]:40']
          listen_addresses = ['127.0.0.2:53', '[::1]:53']


          As you can see, an exact match is needed to perform the replace.






          share|improve this answer












          You can do:



          sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file



          • Searches for the exact string listen_addresses = ['127.0.0.1:53', '[::1]:53']

          • If the string matches s/53/40/g will replace all occurences of 53 with 40 (but only in the matched line)


          Example output:



          $ cat file
          listen_addresses = ['127.0.0.1:53', '[::1]:53']
          listen_addresses = ['127.0.0.2:53', '[::1]:53']

          $ sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
          listen_addresses = ['127.0.0.1:40', '[::1]:40']
          listen_addresses = ['127.0.0.2:53', '[::1]:53']


          As you can see, an exact match is needed to perform the replace.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 3 at 12:24









          chaos

          34.1k770113




          34.1k770113




















              up vote
              0
              down vote













              sed s/":53'"/":40'"/g <<<"listen_addresses = ['127.0.0.1:53', '[::1]:53']"


              do something close to you asked. But I agree with comment of @Jeff Schaller!



              From your question nobody can recognize why do you simply do not it by hand? What is the text from which you get your lines, how they differs each other? what variants should be covered by regex?



              sed is good for replacing but for electing the coresct line the grep is better.






              share|improve this answer
























                up vote
                0
                down vote













                sed s/":53'"/":40'"/g <<<"listen_addresses = ['127.0.0.1:53', '[::1]:53']"


                do something close to you asked. But I agree with comment of @Jeff Schaller!



                From your question nobody can recognize why do you simply do not it by hand? What is the text from which you get your lines, how they differs each other? what variants should be covered by regex?



                sed is good for replacing but for electing the coresct line the grep is better.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  sed s/":53'"/":40'"/g <<<"listen_addresses = ['127.0.0.1:53', '[::1]:53']"


                  do something close to you asked. But I agree with comment of @Jeff Schaller!



                  From your question nobody can recognize why do you simply do not it by hand? What is the text from which you get your lines, how they differs each other? what variants should be covered by regex?



                  sed is good for replacing but for electing the coresct line the grep is better.






                  share|improve this answer












                  sed s/":53'"/":40'"/g <<<"listen_addresses = ['127.0.0.1:53', '[::1]:53']"


                  do something close to you asked. But I agree with comment of @Jeff Schaller!



                  From your question nobody can recognize why do you simply do not it by hand? What is the text from which you get your lines, how they differs each other? what variants should be covered by regex?



                  sed is good for replacing but for electing the coresct line the grep is better.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 3 at 12:43









                  schweik

                  1804




                  1804



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466562%2freplace-text-with-another%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?