How to join a line with a pattern with the next line with sed?












0














I can't find this case in the board, so I'm asking the question.



This is input file:



module  
x(a,b,c)
module
y(d,e,f,
g,h,i)
module
z(j,k,l)


And output file should be:



module x(a,b,c)  
module y(d,e,f,
g,h,i)
module z(j,k,l)









share|improve this question





























    0














    I can't find this case in the board, so I'm asking the question.



    This is input file:



    module  
    x(a,b,c)
    module
    y(d,e,f,
    g,h,i)
    module
    z(j,k,l)


    And output file should be:



    module x(a,b,c)  
    module y(d,e,f,
    g,h,i)
    module z(j,k,l)









    share|improve this question



























      0












      0








      0







      I can't find this case in the board, so I'm asking the question.



      This is input file:



      module  
      x(a,b,c)
      module
      y(d,e,f,
      g,h,i)
      module
      z(j,k,l)


      And output file should be:



      module x(a,b,c)  
      module y(d,e,f,
      g,h,i)
      module z(j,k,l)









      share|improve this question















      I can't find this case in the board, so I'm asking the question.



      This is input file:



      module  
      x(a,b,c)
      module
      y(d,e,f,
      g,h,i)
      module
      z(j,k,l)


      And output file should be:



      module x(a,b,c)  
      module y(d,e,f,
      g,h,i)
      module z(j,k,l)






      text-processing sed join






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 17 at 6:34









      αғsнιη

      16.5k102865




      16.5k102865










      asked Dec 17 at 6:24









      funfun

      112




      112






















          2 Answers
          2






          active

          oldest

          votes


















          2














          What you want to do is to join the module lines with the next line.



          Using sed:



          $ sed '/^module/N;s/n//' file
          module x(a,b,c)
          module y(d,e,f,
          g,h,i)
          module z(j,k,l)


          This is with your data copied and pasted as is, with spaces at the end of each line.



          The sed command will print each line as it is read, but when it encounters a line that starts with the string module, it appends the next line with an embedded newline character in-between (this is what N does). We remove that newline character with a substitution before the result is printed.



          If your data has no spaces at the end of the lines, use



          $ sed '/^module/N;s/n/ /' file
          module x(a,b,c)
          module y(d,e,f,
          g,h,i)
          module z(j,k,l)




          Just in case you'd want this (assuming no spaces at end of input lines):



          $ sed -e '/^module/bpp' -e 'H;$bpp' -e 'd' 
          -e ':pp' -e 'x;/^$/d;s/n/ /g' file
          module x(a,b,c)
          module y(d,e,f, g,h,i)
          module z(j,k,l)


          Annotated sed script:



          /^module/ b print_previous; # print previous record
          H; # append this line to hold space
          $ b print_previous; # print previous (last) record
          d; # end processing this line

          :print_previous; # prints a record accumulated in the hold space
          x; # swap in the hold space
          /^$/ d; # if line is empty, delete it
          s/n/ /g; # replace embedded newlines by spaces
          # (implicit print)





          share|improve this answer































            1














            Using awk:



            ~ awk '/^module/ {l = $0; getline; printf "%s", l} 1' input-file
            module x(a,b,c)
            module y(d,e,f,
            g,h,i)
            module z(j,k,l)


            For each line that starts with module, save the line in l, move to the next line (getline), and print the saved line without a newline. Then print every line.






            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/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%2f489410%2fhow-to-join-a-line-with-a-pattern-with-the-next-line-with-sed%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              What you want to do is to join the module lines with the next line.



              Using sed:



              $ sed '/^module/N;s/n//' file
              module x(a,b,c)
              module y(d,e,f,
              g,h,i)
              module z(j,k,l)


              This is with your data copied and pasted as is, with spaces at the end of each line.



              The sed command will print each line as it is read, but when it encounters a line that starts with the string module, it appends the next line with an embedded newline character in-between (this is what N does). We remove that newline character with a substitution before the result is printed.



              If your data has no spaces at the end of the lines, use



              $ sed '/^module/N;s/n/ /' file
              module x(a,b,c)
              module y(d,e,f,
              g,h,i)
              module z(j,k,l)




              Just in case you'd want this (assuming no spaces at end of input lines):



              $ sed -e '/^module/bpp' -e 'H;$bpp' -e 'd' 
              -e ':pp' -e 'x;/^$/d;s/n/ /g' file
              module x(a,b,c)
              module y(d,e,f, g,h,i)
              module z(j,k,l)


              Annotated sed script:



              /^module/ b print_previous; # print previous record
              H; # append this line to hold space
              $ b print_previous; # print previous (last) record
              d; # end processing this line

              :print_previous; # prints a record accumulated in the hold space
              x; # swap in the hold space
              /^$/ d; # if line is empty, delete it
              s/n/ /g; # replace embedded newlines by spaces
              # (implicit print)





              share|improve this answer




























                2














                What you want to do is to join the module lines with the next line.



                Using sed:



                $ sed '/^module/N;s/n//' file
                module x(a,b,c)
                module y(d,e,f,
                g,h,i)
                module z(j,k,l)


                This is with your data copied and pasted as is, with spaces at the end of each line.



                The sed command will print each line as it is read, but when it encounters a line that starts with the string module, it appends the next line with an embedded newline character in-between (this is what N does). We remove that newline character with a substitution before the result is printed.



                If your data has no spaces at the end of the lines, use



                $ sed '/^module/N;s/n/ /' file
                module x(a,b,c)
                module y(d,e,f,
                g,h,i)
                module z(j,k,l)




                Just in case you'd want this (assuming no spaces at end of input lines):



                $ sed -e '/^module/bpp' -e 'H;$bpp' -e 'd' 
                -e ':pp' -e 'x;/^$/d;s/n/ /g' file
                module x(a,b,c)
                module y(d,e,f, g,h,i)
                module z(j,k,l)


                Annotated sed script:



                /^module/ b print_previous; # print previous record
                H; # append this line to hold space
                $ b print_previous; # print previous (last) record
                d; # end processing this line

                :print_previous; # prints a record accumulated in the hold space
                x; # swap in the hold space
                /^$/ d; # if line is empty, delete it
                s/n/ /g; # replace embedded newlines by spaces
                # (implicit print)





                share|improve this answer


























                  2












                  2








                  2






                  What you want to do is to join the module lines with the next line.



                  Using sed:



                  $ sed '/^module/N;s/n//' file
                  module x(a,b,c)
                  module y(d,e,f,
                  g,h,i)
                  module z(j,k,l)


                  This is with your data copied and pasted as is, with spaces at the end of each line.



                  The sed command will print each line as it is read, but when it encounters a line that starts with the string module, it appends the next line with an embedded newline character in-between (this is what N does). We remove that newline character with a substitution before the result is printed.



                  If your data has no spaces at the end of the lines, use



                  $ sed '/^module/N;s/n/ /' file
                  module x(a,b,c)
                  module y(d,e,f,
                  g,h,i)
                  module z(j,k,l)




                  Just in case you'd want this (assuming no spaces at end of input lines):



                  $ sed -e '/^module/bpp' -e 'H;$bpp' -e 'd' 
                  -e ':pp' -e 'x;/^$/d;s/n/ /g' file
                  module x(a,b,c)
                  module y(d,e,f, g,h,i)
                  module z(j,k,l)


                  Annotated sed script:



                  /^module/ b print_previous; # print previous record
                  H; # append this line to hold space
                  $ b print_previous; # print previous (last) record
                  d; # end processing this line

                  :print_previous; # prints a record accumulated in the hold space
                  x; # swap in the hold space
                  /^$/ d; # if line is empty, delete it
                  s/n/ /g; # replace embedded newlines by spaces
                  # (implicit print)





                  share|improve this answer














                  What you want to do is to join the module lines with the next line.



                  Using sed:



                  $ sed '/^module/N;s/n//' file
                  module x(a,b,c)
                  module y(d,e,f,
                  g,h,i)
                  module z(j,k,l)


                  This is with your data copied and pasted as is, with spaces at the end of each line.



                  The sed command will print each line as it is read, but when it encounters a line that starts with the string module, it appends the next line with an embedded newline character in-between (this is what N does). We remove that newline character with a substitution before the result is printed.



                  If your data has no spaces at the end of the lines, use



                  $ sed '/^module/N;s/n/ /' file
                  module x(a,b,c)
                  module y(d,e,f,
                  g,h,i)
                  module z(j,k,l)




                  Just in case you'd want this (assuming no spaces at end of input lines):



                  $ sed -e '/^module/bpp' -e 'H;$bpp' -e 'd' 
                  -e ':pp' -e 'x;/^$/d;s/n/ /g' file
                  module x(a,b,c)
                  module y(d,e,f, g,h,i)
                  module z(j,k,l)


                  Annotated sed script:



                  /^module/ b print_previous; # print previous record
                  H; # append this line to hold space
                  $ b print_previous; # print previous (last) record
                  d; # end processing this line

                  :print_previous; # prints a record accumulated in the hold space
                  x; # swap in the hold space
                  /^$/ d; # if line is empty, delete it
                  s/n/ /g; # replace embedded newlines by spaces
                  # (implicit print)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 17 at 10:46

























                  answered Dec 17 at 6:52









                  Kusalananda

                  121k16229372




                  121k16229372

























                      1














                      Using awk:



                      ~ awk '/^module/ {l = $0; getline; printf "%s", l} 1' input-file
                      module x(a,b,c)
                      module y(d,e,f,
                      g,h,i)
                      module z(j,k,l)


                      For each line that starts with module, save the line in l, move to the next line (getline), and print the saved line without a newline. Then print every line.






                      share|improve this answer


























                        1














                        Using awk:



                        ~ awk '/^module/ {l = $0; getline; printf "%s", l} 1' input-file
                        module x(a,b,c)
                        module y(d,e,f,
                        g,h,i)
                        module z(j,k,l)


                        For each line that starts with module, save the line in l, move to the next line (getline), and print the saved line without a newline. Then print every line.






                        share|improve this answer
























                          1












                          1








                          1






                          Using awk:



                          ~ awk '/^module/ {l = $0; getline; printf "%s", l} 1' input-file
                          module x(a,b,c)
                          module y(d,e,f,
                          g,h,i)
                          module z(j,k,l)


                          For each line that starts with module, save the line in l, move to the next line (getline), and print the saved line without a newline. Then print every line.






                          share|improve this answer












                          Using awk:



                          ~ awk '/^module/ {l = $0; getline; printf "%s", l} 1' input-file
                          module x(a,b,c)
                          module y(d,e,f,
                          g,h,i)
                          module z(j,k,l)


                          For each line that starts with module, save the line in l, move to the next line (getline), and print the saved line without a newline. Then print every line.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 17 at 6:52









                          muru

                          1




                          1






























                              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%2f489410%2fhow-to-join-a-line-with-a-pattern-with-the-next-line-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

                              Morgemoulin

                              Scott Moir

                              Souastre