How to insert multiple lines with sed












7














I want to add this



#this 
##is my
text


before the line



the specific line 


I tried this



sed -i '/the specific line/i 
#this
##is my
text
' text.txt


but it only adds in 'text'.



I also tried various combinations with and " " but nothing worked.










share|improve this question





























    7














    I want to add this



    #this 
    ##is my
    text


    before the line



    the specific line 


    I tried this



    sed -i '/the specific line/i 
    #this
    ##is my
    text
    ' text.txt


    but it only adds in 'text'.



    I also tried various combinations with and " " but nothing worked.










    share|improve this question



























      7












      7








      7


      3





      I want to add this



      #this 
      ##is my
      text


      before the line



      the specific line 


      I tried this



      sed -i '/the specific line/i 
      #this
      ##is my
      text
      ' text.txt


      but it only adds in 'text'.



      I also tried various combinations with and " " but nothing worked.










      share|improve this question















      I want to add this



      #this 
      ##is my
      text


      before the line



      the specific line 


      I tried this



      sed -i '/the specific line/i 
      #this
      ##is my
      text
      ' text.txt


      but it only adds in 'text'.



      I also tried various combinations with and " " but nothing worked.







      bash sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '15 at 8:54









      kos

      25.3k870119




      25.3k870119










      asked Nov 26 '15 at 7:33









      Paul Bernhard Wagner

      2102511




      2102511






















          3 Answers
          3






          active

          oldest

          votes


















          2














          With newlines:



          % sed -i '/the specific line/i #thisn##is myntext' foo

          % cat foo
          #this
          ##is my
          text
          the specific line





          share|improve this answer





























            6














            You're missing the trailing backslash at the end of some lines (and you have an eccessive newline at the end of the last line you want to insert):



            sed -i '/the specific line/i 
            #this
            ##is my
            text' file


            % cat file
            foo
            the specific line
            bar

            % sed -i '/the specific line/i
            #this
            ##is my
            text' file

            % cat file
            foo
            #this
            ##is my
            text
            the specific line
            bar





            share|improve this answer































              1














              When the replacement string has newlines and spaces, you can use something else. We will try to insert the output of ls -l in the middle of some template file.



              awk 'NR==FNR {a[NR]=$0;next}
              /Insert index here/ {for (i=1; i <= length(a); i++) { print a[i] }}
              {print}'
              <(ls -l) text.txt


              When you want something inserted after a line, you can move the command {print} or switch to :



              sed '/Insert command output after this line/r'<(ls -l) text.txt


              You can also use sed for inserting before a line with



              sed 's/Insert command output after this line/ls -l; echo "&"/e' text.txt





              share|improve this answer










              New contributor




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


















                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',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                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%2faskubuntu.com%2fquestions%2f702677%2fhow-to-insert-multiple-lines-with-sed%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









                2














                With newlines:



                % sed -i '/the specific line/i #thisn##is myntext' foo

                % cat foo
                #this
                ##is my
                text
                the specific line





                share|improve this answer


























                  2














                  With newlines:



                  % sed -i '/the specific line/i #thisn##is myntext' foo

                  % cat foo
                  #this
                  ##is my
                  text
                  the specific line





                  share|improve this answer
























                    2












                    2








                    2






                    With newlines:



                    % sed -i '/the specific line/i #thisn##is myntext' foo

                    % cat foo
                    #this
                    ##is my
                    text
                    the specific line





                    share|improve this answer












                    With newlines:



                    % sed -i '/the specific line/i #thisn##is myntext' foo

                    % cat foo
                    #this
                    ##is my
                    text
                    the specific line






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 26 '15 at 8:14









                    A.B.

                    68k12165255




                    68k12165255

























                        6














                        You're missing the trailing backslash at the end of some lines (and you have an eccessive newline at the end of the last line you want to insert):



                        sed -i '/the specific line/i 
                        #this
                        ##is my
                        text' file


                        % cat file
                        foo
                        the specific line
                        bar

                        % sed -i '/the specific line/i
                        #this
                        ##is my
                        text' file

                        % cat file
                        foo
                        #this
                        ##is my
                        text
                        the specific line
                        bar





                        share|improve this answer




























                          6














                          You're missing the trailing backslash at the end of some lines (and you have an eccessive newline at the end of the last line you want to insert):



                          sed -i '/the specific line/i 
                          #this
                          ##is my
                          text' file


                          % cat file
                          foo
                          the specific line
                          bar

                          % sed -i '/the specific line/i
                          #this
                          ##is my
                          text' file

                          % cat file
                          foo
                          #this
                          ##is my
                          text
                          the specific line
                          bar





                          share|improve this answer


























                            6












                            6








                            6






                            You're missing the trailing backslash at the end of some lines (and you have an eccessive newline at the end of the last line you want to insert):



                            sed -i '/the specific line/i 
                            #this
                            ##is my
                            text' file


                            % cat file
                            foo
                            the specific line
                            bar

                            % sed -i '/the specific line/i
                            #this
                            ##is my
                            text' file

                            % cat file
                            foo
                            #this
                            ##is my
                            text
                            the specific line
                            bar





                            share|improve this answer














                            You're missing the trailing backslash at the end of some lines (and you have an eccessive newline at the end of the last line you want to insert):



                            sed -i '/the specific line/i 
                            #this
                            ##is my
                            text' file


                            % cat file
                            foo
                            the specific line
                            bar

                            % sed -i '/the specific line/i
                            #this
                            ##is my
                            text' file

                            % cat file
                            foo
                            #this
                            ##is my
                            text
                            the specific line
                            bar






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 26 '15 at 8:51









                            A.B.

                            68k12165255




                            68k12165255










                            answered Nov 26 '15 at 8:29









                            kos

                            25.3k870119




                            25.3k870119























                                1














                                When the replacement string has newlines and spaces, you can use something else. We will try to insert the output of ls -l in the middle of some template file.



                                awk 'NR==FNR {a[NR]=$0;next}
                                /Insert index here/ {for (i=1; i <= length(a); i++) { print a[i] }}
                                {print}'
                                <(ls -l) text.txt


                                When you want something inserted after a line, you can move the command {print} or switch to :



                                sed '/Insert command output after this line/r'<(ls -l) text.txt


                                You can also use sed for inserting before a line with



                                sed 's/Insert command output after this line/ls -l; echo "&"/e' text.txt





                                share|improve this answer










                                New contributor




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























                                  1














                                  When the replacement string has newlines and spaces, you can use something else. We will try to insert the output of ls -l in the middle of some template file.



                                  awk 'NR==FNR {a[NR]=$0;next}
                                  /Insert index here/ {for (i=1; i <= length(a); i++) { print a[i] }}
                                  {print}'
                                  <(ls -l) text.txt


                                  When you want something inserted after a line, you can move the command {print} or switch to :



                                  sed '/Insert command output after this line/r'<(ls -l) text.txt


                                  You can also use sed for inserting before a line with



                                  sed 's/Insert command output after this line/ls -l; echo "&"/e' text.txt





                                  share|improve this answer










                                  New contributor




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





















                                    1












                                    1








                                    1






                                    When the replacement string has newlines and spaces, you can use something else. We will try to insert the output of ls -l in the middle of some template file.



                                    awk 'NR==FNR {a[NR]=$0;next}
                                    /Insert index here/ {for (i=1; i <= length(a); i++) { print a[i] }}
                                    {print}'
                                    <(ls -l) text.txt


                                    When you want something inserted after a line, you can move the command {print} or switch to :



                                    sed '/Insert command output after this line/r'<(ls -l) text.txt


                                    You can also use sed for inserting before a line with



                                    sed 's/Insert command output after this line/ls -l; echo "&"/e' text.txt





                                    share|improve this answer










                                    New contributor




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









                                    When the replacement string has newlines and spaces, you can use something else. We will try to insert the output of ls -l in the middle of some template file.



                                    awk 'NR==FNR {a[NR]=$0;next}
                                    /Insert index here/ {for (i=1; i <= length(a); i++) { print a[i] }}
                                    {print}'
                                    <(ls -l) text.txt


                                    When you want something inserted after a line, you can move the command {print} or switch to :



                                    sed '/Insert command output after this line/r'<(ls -l) text.txt


                                    You can also use sed for inserting before a line with



                                    sed 's/Insert command output after this line/ls -l; echo "&"/e' text.txt






                                    share|improve this answer










                                    New contributor




                                    Walter A 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 answer



                                    share|improve this answer








                                    edited Jan 2 at 17:05





















                                    New contributor




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









                                    answered Dec 31 '18 at 11:42









                                    Walter A

                                    1113




                                    1113




                                    New contributor




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





                                    New contributor





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






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






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Ask Ubuntu!


                                        • 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%2faskubuntu.com%2fquestions%2f702677%2fhow-to-insert-multiple-lines-with-sed%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

                                        Human spaceflight

                                        Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

                                        張江高科駅