Using “sed” to append to end of a filewriting a command transcript to fileCreate a file as a different user and groupAppend at the end of line using sedsed - calling a variable from a file with multilineStandard Out Append to File Size LimitationsWhere should I put ownerless log file-w flag in bash if statement not working

Why are Starfleet vessels designed with nacelles so far away from the hull?

If the music alphabet had more than 7 letters would octaves still sound like the same note?

How can demon technology be prevented from surpassing humans?

How to remind myself to lock my doors

An idiomatic word for "very little" in this context?

Is data science mathematically interesting?

How did Ron get five hundred Chocolate Frog cards?

Glacial, Magnetic and Mossy Lures; what Pokémon do they attract?

Does wall of stone need support or not?

How to execute a project with two resources where you need three resources?

Where is a "fat Cantor staircase" differentiable?

How should I tell a professor the answer to something he doesn't know?

Why is it so hard to land on the Moon?

Drawing Super Mario Bros.....in LaTeX

Giving a character trauma but not "diagnosing" her?

How can I attach a set of five panniers?

I didn't do any exit passport control when leaving Japan. What should I do?

Google can't fetch large sitemap with 50k URLs, nor will browsers render it

Why does 1.1.1.1 not resolve archive.is?

Match the blocks

Would removing the "total cover" part of a Paladin's Divine Sense unbalance the feature?

My professor says my digit summing code is flawed. Is he right?

'Kukhtarev's model' or 'THE Kukhtarev's model'?

Did smallpox emerge in 1580?



Using “sed” to append to end of a file


writing a command transcript to fileCreate a file as a different user and groupAppend at the end of line using sedsed - calling a variable from a file with multilineStandard Out Append to File Size LimitationsWhere should I put ownerless log file-w flag in bash if statement not working






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









2

















I am currently using sed to write to an apache configuration file from stdin. I am using sed in this script to get around the bash script limition where the calling user does not have privilegess to write to the file, so I can't simply echo "..." >> outputfile.conf



Here is what I have for writing to the file:



echo "<VirtualHost *:80>
...
</VirtualHost>" | sudo sed -n "w/etc/apache2/sites-available/000-default.conf"


How can I later append to this same file?



if $enable_ssl; then
echo "<VirtualHost *:443>
...
</VirtualHost>" | sudo sed <opions-to-sed-here>
fi









share|improve this question









New contributor



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


















  • 2





    Could you simply use tee -a?

    – Arkadiusz Drabczyk
    8 hours ago

















2

















I am currently using sed to write to an apache configuration file from stdin. I am using sed in this script to get around the bash script limition where the calling user does not have privilegess to write to the file, so I can't simply echo "..." >> outputfile.conf



Here is what I have for writing to the file:



echo "<VirtualHost *:80>
...
</VirtualHost>" | sudo sed -n "w/etc/apache2/sites-available/000-default.conf"


How can I later append to this same file?



if $enable_ssl; then
echo "<VirtualHost *:443>
...
</VirtualHost>" | sudo sed <opions-to-sed-here>
fi









share|improve this question









New contributor



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


















  • 2





    Could you simply use tee -a?

    – Arkadiusz Drabczyk
    8 hours ago













2












2








2








I am currently using sed to write to an apache configuration file from stdin. I am using sed in this script to get around the bash script limition where the calling user does not have privilegess to write to the file, so I can't simply echo "..." >> outputfile.conf



Here is what I have for writing to the file:



echo "<VirtualHost *:80>
...
</VirtualHost>" | sudo sed -n "w/etc/apache2/sites-available/000-default.conf"


How can I later append to this same file?



if $enable_ssl; then
echo "<VirtualHost *:443>
...
</VirtualHost>" | sudo sed <opions-to-sed-here>
fi









share|improve this question









New contributor



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











I am currently using sed to write to an apache configuration file from stdin. I am using sed in this script to get around the bash script limition where the calling user does not have privilegess to write to the file, so I can't simply echo "..." >> outputfile.conf



Here is what I have for writing to the file:



echo "<VirtualHost *:80>
...
</VirtualHost>" | sudo sed -n "w/etc/apache2/sites-available/000-default.conf"


How can I later append to this same file?



if $enable_ssl; then
echo "<VirtualHost *:443>
...
</VirtualHost>" | sudo sed <opions-to-sed-here>
fi






bash shell-script sed






share|improve this question









New contributor



Maverik Minett 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



Maverik Minett 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



share|improve this question








edited 8 hours ago







Maverik Minett













New contributor



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








asked 8 hours ago









Maverik MinettMaverik Minett

112 bronze badges




112 bronze badges




New contributor



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




New contributor




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












  • 2





    Could you simply use tee -a?

    – Arkadiusz Drabczyk
    8 hours ago












  • 2





    Could you simply use tee -a?

    – Arkadiusz Drabczyk
    8 hours ago







2




2





Could you simply use tee -a?

– Arkadiusz Drabczyk
8 hours ago





Could you simply use tee -a?

– Arkadiusz Drabczyk
8 hours ago










3 Answers
3






active

oldest

votes


















3


















The usual replacement for shell > with higher/different privileges is:



echo "replace file content with this line" | sudo tee protectedFile >/dev/null


And if you want to append, use -a:



echo "append this line to file" | sudo tee -a protectedFile >/dev/null





share|improve this answer

































    2


















    Whithout echo:



    string=$'"<VirtualHost *:443> 
    ...
    </VirtualHost>"'

    sudo sed -i '$a'"$string"'' file


    The -i option tells sed to process a files in-place (optionally adding a suffix to the original version).



    The sed script:




    1. $ match last line


    2. a append text after matching line

    It won't work on an empty file though (since there won't be any lines of input to match the last line).






    share|improve this answer




























    • I'm not sure why you have the double single quote at the end of the sed command?

      – Grump
      5 hours ago


















    0


















    For completeness, if you have ed at hand:



    echo "$
    a
    <VirtualHost *:80>
    ...
    </VirtualHost>
    .
    w" | sudo ed protectedFile



    1. $ go to last line


    2. a append, followed by data you want to append terminated by a line containing a single dot


    3. w stands for write (file)

    The first two commands can be concatenated (yielding $a), yet this won't work on empty file – separately it will, since it means "go to last line" (which is a no-op), followed by "append" rather than "append to last line", which doesn't really exist.






    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',
      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/4.0/"u003ecc by-sa 4.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
      );



      );







      Maverik Minett 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%2funix.stackexchange.com%2fquestions%2f545149%2fusing-sed-to-append-to-end-of-a-file%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown


























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3


















      The usual replacement for shell > with higher/different privileges is:



      echo "replace file content with this line" | sudo tee protectedFile >/dev/null


      And if you want to append, use -a:



      echo "append this line to file" | sudo tee -a protectedFile >/dev/null





      share|improve this answer






























        3


















        The usual replacement for shell > with higher/different privileges is:



        echo "replace file content with this line" | sudo tee protectedFile >/dev/null


        And if you want to append, use -a:



        echo "append this line to file" | sudo tee -a protectedFile >/dev/null





        share|improve this answer




























          3














          3










          3









          The usual replacement for shell > with higher/different privileges is:



          echo "replace file content with this line" | sudo tee protectedFile >/dev/null


          And if you want to append, use -a:



          echo "append this line to file" | sudo tee -a protectedFile >/dev/null





          share|improve this answer














          The usual replacement for shell > with higher/different privileges is:



          echo "replace file content with this line" | sudo tee protectedFile >/dev/null


          And if you want to append, use -a:



          echo "append this line to file" | sudo tee -a protectedFile >/dev/null






          share|improve this answer













          share|improve this answer




          share|improve this answer



          share|improve this answer










          answered 5 hours ago









          IsaacIsaac

          15.1k1 gold badge25 silver badges64 bronze badges




          15.1k1 gold badge25 silver badges64 bronze badges


























              2


















              Whithout echo:



              string=$'"<VirtualHost *:443> 
              ...
              </VirtualHost>"'

              sudo sed -i '$a'"$string"'' file


              The -i option tells sed to process a files in-place (optionally adding a suffix to the original version).



              The sed script:




              1. $ match last line


              2. a append text after matching line

              It won't work on an empty file though (since there won't be any lines of input to match the last line).






              share|improve this answer




























              • I'm not sure why you have the double single quote at the end of the sed command?

                – Grump
                5 hours ago















              2


















              Whithout echo:



              string=$'"<VirtualHost *:443> 
              ...
              </VirtualHost>"'

              sudo sed -i '$a'"$string"'' file


              The -i option tells sed to process a files in-place (optionally adding a suffix to the original version).



              The sed script:




              1. $ match last line


              2. a append text after matching line

              It won't work on an empty file though (since there won't be any lines of input to match the last line).






              share|improve this answer




























              • I'm not sure why you have the double single quote at the end of the sed command?

                – Grump
                5 hours ago













              2














              2










              2









              Whithout echo:



              string=$'"<VirtualHost *:443> 
              ...
              </VirtualHost>"'

              sudo sed -i '$a'"$string"'' file


              The -i option tells sed to process a files in-place (optionally adding a suffix to the original version).



              The sed script:




              1. $ match last line


              2. a append text after matching line

              It won't work on an empty file though (since there won't be any lines of input to match the last line).






              share|improve this answer
















              Whithout echo:



              string=$'"<VirtualHost *:443> 
              ...
              </VirtualHost>"'

              sudo sed -i '$a'"$string"'' file


              The -i option tells sed to process a files in-place (optionally adding a suffix to the original version).



              The sed script:




              1. $ match last line


              2. a append text after matching line

              It won't work on an empty file though (since there won't be any lines of input to match the last line).







              share|improve this answer















              share|improve this answer




              share|improve this answer



              share|improve this answer








              edited 5 hours ago









              peterph

              24.5k2 gold badges49 silver badges63 bronze badges




              24.5k2 gold badges49 silver badges63 bronze badges










              answered 6 hours ago









              guillermo chamorroguillermo chamorro

              9391 silver badge14 bronze badges




              9391 silver badge14 bronze badges















              • I'm not sure why you have the double single quote at the end of the sed command?

                – Grump
                5 hours ago

















              • I'm not sure why you have the double single quote at the end of the sed command?

                – Grump
                5 hours ago
















              I'm not sure why you have the double single quote at the end of the sed command?

              – Grump
              5 hours ago





              I'm not sure why you have the double single quote at the end of the sed command?

              – Grump
              5 hours ago











              0


















              For completeness, if you have ed at hand:



              echo "$
              a
              <VirtualHost *:80>
              ...
              </VirtualHost>
              .
              w" | sudo ed protectedFile



              1. $ go to last line


              2. a append, followed by data you want to append terminated by a line containing a single dot


              3. w stands for write (file)

              The first two commands can be concatenated (yielding $a), yet this won't work on empty file – separately it will, since it means "go to last line" (which is a no-op), followed by "append" rather than "append to last line", which doesn't really exist.






              share|improve this answer






























                0


















                For completeness, if you have ed at hand:



                echo "$
                a
                <VirtualHost *:80>
                ...
                </VirtualHost>
                .
                w" | sudo ed protectedFile



                1. $ go to last line


                2. a append, followed by data you want to append terminated by a line containing a single dot


                3. w stands for write (file)

                The first two commands can be concatenated (yielding $a), yet this won't work on empty file – separately it will, since it means "go to last line" (which is a no-op), followed by "append" rather than "append to last line", which doesn't really exist.






                share|improve this answer




























                  0














                  0










                  0









                  For completeness, if you have ed at hand:



                  echo "$
                  a
                  <VirtualHost *:80>
                  ...
                  </VirtualHost>
                  .
                  w" | sudo ed protectedFile



                  1. $ go to last line


                  2. a append, followed by data you want to append terminated by a line containing a single dot


                  3. w stands for write (file)

                  The first two commands can be concatenated (yielding $a), yet this won't work on empty file – separately it will, since it means "go to last line" (which is a no-op), followed by "append" rather than "append to last line", which doesn't really exist.






                  share|improve this answer














                  For completeness, if you have ed at hand:



                  echo "$
                  a
                  <VirtualHost *:80>
                  ...
                  </VirtualHost>
                  .
                  w" | sudo ed protectedFile



                  1. $ go to last line


                  2. a append, followed by data you want to append terminated by a line containing a single dot


                  3. w stands for write (file)

                  The first two commands can be concatenated (yielding $a), yet this won't work on empty file – separately it will, since it means "go to last line" (which is a no-op), followed by "append" rather than "append to last line", which doesn't really exist.







                  share|improve this answer













                  share|improve this answer




                  share|improve this answer



                  share|improve this answer










                  answered 4 hours ago









                  peterphpeterph

                  24.5k2 gold badges49 silver badges63 bronze badges




                  24.5k2 gold badges49 silver badges63 bronze badges
























                      Maverik Minett is a new contributor. Be nice, and check out our Code of Conduct.









                      draft saved

                      draft discarded

















                      Maverik Minett is a new contributor. Be nice, and check out our Code of Conduct.












                      Maverik Minett is a new contributor. Be nice, and check out our Code of Conduct.











                      Maverik Minett is a new contributor. Be nice, and check out our Code of Conduct.














                      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%2f545149%2fusing-sed-to-append-to-end-of-a-file%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

                      Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                      Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                      Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її