How to insert multiple lines with sed












8















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





























    8















    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



























      8












      8








      8


      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.5k870121




      25.5k870121










      asked Nov 26 '15 at 7:33









      Paul Bernhard WagnerPaul Bernhard Wagner

      2152511




      2152511






















          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

























                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.A.B.

                    68.5k12168258




                    68.5k12168258

























                        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.

                            68.5k12168258




                            68.5k12168258










                            answered Nov 26 '15 at 8:29









                            koskos

                            25.5k870121




                            25.5k870121























                                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






























                                  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




























                                    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















                                    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














                                    share|improve this answer



                                    share|improve this answer








                                    edited Jan 2 at 17:05

























                                    answered Dec 31 '18 at 11:42









                                    Walter AWalter A

                                    1113




                                    1113






























                                        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.




                                        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

                                        How to change which sound is reproduced for terminal bell?

                                        Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                        Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents