sed : replace 1 word without repeating the rest of the sentence

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











up vote
1
down vote

favorite












I have the following pattern :



foo bar


with foo being always the same, but bar can change. I have an hash table that, for each value of bar, has a new one. What I do now is:



sed -i 's/foo bar/foo new_value/g' file


I am pretty sure there is a way to avoid repeating the foo in the replacement string. Does sed provide something like this ? Like



sed -i 's/foo @@bar@@/new_value/g' file


@@string@@ being the value that has to be replaced.










share|improve this question



























    up vote
    1
    down vote

    favorite












    I have the following pattern :



    foo bar


    with foo being always the same, but bar can change. I have an hash table that, for each value of bar, has a new one. What I do now is:



    sed -i 's/foo bar/foo new_value/g' file


    I am pretty sure there is a way to avoid repeating the foo in the replacement string. Does sed provide something like this ? Like



    sed -i 's/foo @@bar@@/new_value/g' file


    @@string@@ being the value that has to be replaced.










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have the following pattern :



      foo bar


      with foo being always the same, but bar can change. I have an hash table that, for each value of bar, has a new one. What I do now is:



      sed -i 's/foo bar/foo new_value/g' file


      I am pretty sure there is a way to avoid repeating the foo in the replacement string. Does sed provide something like this ? Like



      sed -i 's/foo @@bar@@/new_value/g' file


      @@string@@ being the value that has to be replaced.










      share|improve this question















      I have the following pattern :



      foo bar


      with foo being always the same, but bar can change. I have an hash table that, for each value of bar, has a new one. What I do now is:



      sed -i 's/foo bar/foo new_value/g' file


      I am pretty sure there is a way to avoid repeating the foo in the replacement string. Does sed provide something like this ? Like



      sed -i 's/foo @@bar@@/new_value/g' file


      @@string@@ being the value that has to be replaced.







      sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 at 15:13









      Rui F Ribeiro

      38.3k1476127




      38.3k1476127










      asked Oct 24 '14 at 9:07









      EddaSnorra

      1132




      1132




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          sed -i 's/(foo )bar/1new_value/g' file



          The ( )sequence "captures" the text which then can be placed in the replacement string as 1. This can be done multiple times, the digit in 1 indicates which captured text to use. Captured text is counted in order of ( order.






          share|improve this answer




















          • Please mark the answer as correct if it works for you :-)
            – wurtel
            Oct 24 '14 at 9:13










          • I will as soon as I can (10 minute delay).
            – EddaSnorra
            Oct 24 '14 at 9:17










          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: 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%2f163958%2fsed-replace-1-word-without-repeating-the-rest-of-the-sentence%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          sed -i 's/(foo )bar/1new_value/g' file



          The ( )sequence "captures" the text which then can be placed in the replacement string as 1. This can be done multiple times, the digit in 1 indicates which captured text to use. Captured text is counted in order of ( order.






          share|improve this answer




















          • Please mark the answer as correct if it works for you :-)
            – wurtel
            Oct 24 '14 at 9:13










          • I will as soon as I can (10 minute delay).
            – EddaSnorra
            Oct 24 '14 at 9:17














          up vote
          0
          down vote



          accepted










          sed -i 's/(foo )bar/1new_value/g' file



          The ( )sequence "captures" the text which then can be placed in the replacement string as 1. This can be done multiple times, the digit in 1 indicates which captured text to use. Captured text is counted in order of ( order.






          share|improve this answer




















          • Please mark the answer as correct if it works for you :-)
            – wurtel
            Oct 24 '14 at 9:13










          • I will as soon as I can (10 minute delay).
            – EddaSnorra
            Oct 24 '14 at 9:17












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          sed -i 's/(foo )bar/1new_value/g' file



          The ( )sequence "captures" the text which then can be placed in the replacement string as 1. This can be done multiple times, the digit in 1 indicates which captured text to use. Captured text is counted in order of ( order.






          share|improve this answer












          sed -i 's/(foo )bar/1new_value/g' file



          The ( )sequence "captures" the text which then can be placed in the replacement string as 1. This can be done multiple times, the digit in 1 indicates which captured text to use. Captured text is counted in order of ( order.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 24 '14 at 9:10









          wurtel

          9,66011325




          9,66011325











          • Please mark the answer as correct if it works for you :-)
            – wurtel
            Oct 24 '14 at 9:13










          • I will as soon as I can (10 minute delay).
            – EddaSnorra
            Oct 24 '14 at 9:17
















          • Please mark the answer as correct if it works for you :-)
            – wurtel
            Oct 24 '14 at 9:13










          • I will as soon as I can (10 minute delay).
            – EddaSnorra
            Oct 24 '14 at 9:17















          Please mark the answer as correct if it works for you :-)
          – wurtel
          Oct 24 '14 at 9:13




          Please mark the answer as correct if it works for you :-)
          – wurtel
          Oct 24 '14 at 9:13












          I will as soon as I can (10 minute delay).
          – EddaSnorra
          Oct 24 '14 at 9:17




          I will as soon as I can (10 minute delay).
          – EddaSnorra
          Oct 24 '14 at 9:17

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f163958%2fsed-replace-1-word-without-repeating-the-rest-of-the-sentence%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 check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay