How to search and replace with sed in a line with variable content in it?












0















I tried to use sed to edit some part of a specific line in a text file and save the change to the original file. OK I now understand that this is not the way sed directly works.
But how can I use sed in a script to edit the part of a line with variable content in it?
I already used sed to change complete lines or used lines as trigger to cut and insert to a new file. But this is different :)
I think what I need is



sed '/baz/s/foo/bar/g'

sed '/beginning of line/s/static text/new static text/g'


This gives me the right output but how can I save it to the source file?
I understand that sed outputs to stoud but when I add a ">> outpufile" it gives me an error message



sed: -e Ausdruck #1, Zeichen 55: Unbekannte Option für `s'


Between static text and new static text is some content that changes from time to time so



sed -n '/static text/new static text/,$p' tmp >> source


will work only for as long as the source content hasn't changed.



Any suggestions?



Greetz










share|improve this question



























    0















    I tried to use sed to edit some part of a specific line in a text file and save the change to the original file. OK I now understand that this is not the way sed directly works.
    But how can I use sed in a script to edit the part of a line with variable content in it?
    I already used sed to change complete lines or used lines as trigger to cut and insert to a new file. But this is different :)
    I think what I need is



    sed '/baz/s/foo/bar/g'

    sed '/beginning of line/s/static text/new static text/g'


    This gives me the right output but how can I save it to the source file?
    I understand that sed outputs to stoud but when I add a ">> outpufile" it gives me an error message



    sed: -e Ausdruck #1, Zeichen 55: Unbekannte Option für `s'


    Between static text and new static text is some content that changes from time to time so



    sed -n '/static text/new static text/,$p' tmp >> source


    will work only for as long as the source content hasn't changed.



    Any suggestions?



    Greetz










    share|improve this question

























      0












      0








      0








      I tried to use sed to edit some part of a specific line in a text file and save the change to the original file. OK I now understand that this is not the way sed directly works.
      But how can I use sed in a script to edit the part of a line with variable content in it?
      I already used sed to change complete lines or used lines as trigger to cut and insert to a new file. But this is different :)
      I think what I need is



      sed '/baz/s/foo/bar/g'

      sed '/beginning of line/s/static text/new static text/g'


      This gives me the right output but how can I save it to the source file?
      I understand that sed outputs to stoud but when I add a ">> outpufile" it gives me an error message



      sed: -e Ausdruck #1, Zeichen 55: Unbekannte Option für `s'


      Between static text and new static text is some content that changes from time to time so



      sed -n '/static text/new static text/,$p' tmp >> source


      will work only for as long as the source content hasn't changed.



      Any suggestions?



      Greetz










      share|improve this question














      I tried to use sed to edit some part of a specific line in a text file and save the change to the original file. OK I now understand that this is not the way sed directly works.
      But how can I use sed in a script to edit the part of a line with variable content in it?
      I already used sed to change complete lines or used lines as trigger to cut and insert to a new file. But this is different :)
      I think what I need is



      sed '/baz/s/foo/bar/g'

      sed '/beginning of line/s/static text/new static text/g'


      This gives me the right output but how can I save it to the source file?
      I understand that sed outputs to stoud but when I add a ">> outpufile" it gives me an error message



      sed: -e Ausdruck #1, Zeichen 55: Unbekannte Option für `s'


      Between static text and new static text is some content that changes from time to time so



      sed -n '/static text/new static text/,$p' tmp >> source


      will work only for as long as the source content hasn't changed.



      Any suggestions?



      Greetz







      shell-script sed






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      diggidrediggidre

      384




      384






















          1 Answer
          1






          active

          oldest

          votes


















          0














          micha@linux-micha: /tmp
          $ echo 'alpha bravo charlie' > blah.txt

          micha@linux-micha: /tmp
          $ cat blah.txt
          alpha bravo charlie

          micha@linux-micha: /tmp
          $ sed -i 's/bravo/delta/' blah.txt

          micha@linux-micha: /tmp
          $ cat blah.txt
          alpha delta charlie





          share|improve this answer
























          • OK it's a bit late here... I'm confused ^^ Can you explain further please

            – diggidre
            2 hours ago













          • Hi, thought you meant in-place editing a file using sed. That's possible with the -i or --in_place flag. See it in my 3rd command above? Regards.

            – Micha
            1 hour ago











          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%2f506373%2fhow-to-search-and-replace-with-sed-in-a-line-with-variable-content-in-it%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









          0














          micha@linux-micha: /tmp
          $ echo 'alpha bravo charlie' > blah.txt

          micha@linux-micha: /tmp
          $ cat blah.txt
          alpha bravo charlie

          micha@linux-micha: /tmp
          $ sed -i 's/bravo/delta/' blah.txt

          micha@linux-micha: /tmp
          $ cat blah.txt
          alpha delta charlie





          share|improve this answer
























          • OK it's a bit late here... I'm confused ^^ Can you explain further please

            – diggidre
            2 hours ago













          • Hi, thought you meant in-place editing a file using sed. That's possible with the -i or --in_place flag. See it in my 3rd command above? Regards.

            – Micha
            1 hour ago
















          0














          micha@linux-micha: /tmp
          $ echo 'alpha bravo charlie' > blah.txt

          micha@linux-micha: /tmp
          $ cat blah.txt
          alpha bravo charlie

          micha@linux-micha: /tmp
          $ sed -i 's/bravo/delta/' blah.txt

          micha@linux-micha: /tmp
          $ cat blah.txt
          alpha delta charlie





          share|improve this answer
























          • OK it's a bit late here... I'm confused ^^ Can you explain further please

            – diggidre
            2 hours ago













          • Hi, thought you meant in-place editing a file using sed. That's possible with the -i or --in_place flag. See it in my 3rd command above? Regards.

            – Micha
            1 hour ago














          0












          0








          0







          micha@linux-micha: /tmp
          $ echo 'alpha bravo charlie' > blah.txt

          micha@linux-micha: /tmp
          $ cat blah.txt
          alpha bravo charlie

          micha@linux-micha: /tmp
          $ sed -i 's/bravo/delta/' blah.txt

          micha@linux-micha: /tmp
          $ cat blah.txt
          alpha delta charlie





          share|improve this answer













          micha@linux-micha: /tmp
          $ echo 'alpha bravo charlie' > blah.txt

          micha@linux-micha: /tmp
          $ cat blah.txt
          alpha bravo charlie

          micha@linux-micha: /tmp
          $ sed -i 's/bravo/delta/' blah.txt

          micha@linux-micha: /tmp
          $ cat blah.txt
          alpha delta charlie






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 hours ago









          MichaMicha

          893




          893













          • OK it's a bit late here... I'm confused ^^ Can you explain further please

            – diggidre
            2 hours ago













          • Hi, thought you meant in-place editing a file using sed. That's possible with the -i or --in_place flag. See it in my 3rd command above? Regards.

            – Micha
            1 hour ago



















          • OK it's a bit late here... I'm confused ^^ Can you explain further please

            – diggidre
            2 hours ago













          • Hi, thought you meant in-place editing a file using sed. That's possible with the -i or --in_place flag. See it in my 3rd command above? Regards.

            – Micha
            1 hour ago

















          OK it's a bit late here... I'm confused ^^ Can you explain further please

          – diggidre
          2 hours ago







          OK it's a bit late here... I'm confused ^^ Can you explain further please

          – diggidre
          2 hours ago















          Hi, thought you meant in-place editing a file using sed. That's possible with the -i or --in_place flag. See it in my 3rd command above? Regards.

          – Micha
          1 hour ago





          Hi, thought you meant in-place editing a file using sed. That's possible with the -i or --in_place flag. See it in my 3rd command above? Regards.

          – Micha
          1 hour ago


















          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%2f506373%2fhow-to-search-and-replace-with-sed-in-a-line-with-variable-content-in-it%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

          Entries order in /etc/network/interfaces

          新発田市

          Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)