Data Manipulation











up vote
-2
down vote

favorite












I have a data as mentioned below.



host_name   Server1.domain.com
contacts DL - Desktop
contact_groups ravi, raj, rahim
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager


Required Output is below.



host_name Server1.domain.com, contacts ravi,raj,rahim, Contact_group DL-Desktop
host_name Server2.domain.com contact_groups DL - Server
host_name Server3.domain.com
host_name Server4.domain.com contacts services,helpdesk,manager









share|improve this question




















  • 1




    This isn't the place to spam links about your website; stop posting them in comments
    – Michael Mrozek
    Jun 8 '16 at 14:39















up vote
-2
down vote

favorite












I have a data as mentioned below.



host_name   Server1.domain.com
contacts DL - Desktop
contact_groups ravi, raj, rahim
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager


Required Output is below.



host_name Server1.domain.com, contacts ravi,raj,rahim, Contact_group DL-Desktop
host_name Server2.domain.com contact_groups DL - Server
host_name Server3.domain.com
host_name Server4.domain.com contacts services,helpdesk,manager









share|improve this question




















  • 1




    This isn't the place to spam links about your website; stop posting them in comments
    – Michael Mrozek
    Jun 8 '16 at 14:39













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have a data as mentioned below.



host_name   Server1.domain.com
contacts DL - Desktop
contact_groups ravi, raj, rahim
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager


Required Output is below.



host_name Server1.domain.com, contacts ravi,raj,rahim, Contact_group DL-Desktop
host_name Server2.domain.com contact_groups DL - Server
host_name Server3.domain.com
host_name Server4.domain.com contacts services,helpdesk,manager









share|improve this question















I have a data as mentioned below.



host_name   Server1.domain.com
contacts DL - Desktop
contact_groups ravi, raj, rahim
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager


Required Output is below.



host_name Server1.domain.com, contacts ravi,raj,rahim, Contact_group DL-Desktop
host_name Server2.domain.com contact_groups DL - Server
host_name Server3.domain.com
host_name Server4.domain.com contacts services,helpdesk,manager






shell-script awk scripting perl gawk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 at 23:40









Rui F Ribeiro

38.3k1477127




38.3k1477127










asked Jun 8 '16 at 6:35









Ravi Ankam

86




86








  • 1




    This isn't the place to spam links about your website; stop posting them in comments
    – Michael Mrozek
    Jun 8 '16 at 14:39














  • 1




    This isn't the place to spam links about your website; stop posting them in comments
    – Michael Mrozek
    Jun 8 '16 at 14:39








1




1




This isn't the place to spam links about your website; stop posting them in comments
– Michael Mrozek
Jun 8 '16 at 14:39




This isn't the place to spam links about your website; stop posting them in comments
– Michael Mrozek
Jun 8 '16 at 14:39










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










I am sure you can do it much easily in awk, but awk doesn't like me much so here is my take on it using everything but the kitchen sink. Assuming that the data is in a file called file1



export output=; while read line; do if [[ "$line" =~ "host_name" ]]; then export output="${output}n"; fi; export output="${output}, $line"; done < file1 && echo -e $output | sed 's/^, ?//' | sed '/^$/d'


Content of file1



host_name   Server1.domain.com
contacts ravi, raj, rahim
contact_groups DL - Desktop
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager


Output of the above command



host_name Server1.domain.com, contacts ravi, raj, rahim, contact_groups DL - Desktop
host_name Server2.domain.com, contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com, contacts Services,helpdesk,manager





share|improve this answer






























    up vote
    1
    down vote













    $ sed -e '2,$ s/^host_name/n&/' ravi.txt | 
    perl -n -e 'if (! m/^$/) {
    chomp;
    $line .= $_ . ", "
    };

    if (m/^$/ || eof) {
    $line =~ s/ +/ /g; # merge multiple spaces into one space
    $line =~ s/, $//; # strip the trailing comma
    print $line,"n" ;
    $line=""
    }'
    host_name Server1.domain.com, contacts DL - Desktop, contact_groups ravi, raj, rahim
    host_name Server2.domain.com, contact_groups DL-Server
    host_name Server3.domain.com
    host_name Server4.domain.com, contacts Services,helpdesk,manager


    First use sed to convert the input to paragraphs (separated by a newline). Then perl to join the lines in each paragraph together and print them out.



    This could be done entirely in perl, but I was lazy and decided it was easier to just convert to paragraphs before piping into a simple perl scipt.






    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',
      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%2f288324%2fdata-manipulation%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








      up vote
      1
      down vote



      accepted










      I am sure you can do it much easily in awk, but awk doesn't like me much so here is my take on it using everything but the kitchen sink. Assuming that the data is in a file called file1



      export output=; while read line; do if [[ "$line" =~ "host_name" ]]; then export output="${output}n"; fi; export output="${output}, $line"; done < file1 && echo -e $output | sed 's/^, ?//' | sed '/^$/d'


      Content of file1



      host_name   Server1.domain.com
      contacts ravi, raj, rahim
      contact_groups DL - Desktop
      host_name Server2.domain.com
      contact_groups DL-Server
      host_name Server3.domain.com
      host_name Server4.domain.com
      contacts Services,helpdesk,manager


      Output of the above command



      host_name Server1.domain.com, contacts ravi, raj, rahim, contact_groups DL - Desktop
      host_name Server2.domain.com, contact_groups DL-Server
      host_name Server3.domain.com
      host_name Server4.domain.com, contacts Services,helpdesk,manager





      share|improve this answer



























        up vote
        1
        down vote



        accepted










        I am sure you can do it much easily in awk, but awk doesn't like me much so here is my take on it using everything but the kitchen sink. Assuming that the data is in a file called file1



        export output=; while read line; do if [[ "$line" =~ "host_name" ]]; then export output="${output}n"; fi; export output="${output}, $line"; done < file1 && echo -e $output | sed 's/^, ?//' | sed '/^$/d'


        Content of file1



        host_name   Server1.domain.com
        contacts ravi, raj, rahim
        contact_groups DL - Desktop
        host_name Server2.domain.com
        contact_groups DL-Server
        host_name Server3.domain.com
        host_name Server4.domain.com
        contacts Services,helpdesk,manager


        Output of the above command



        host_name Server1.domain.com, contacts ravi, raj, rahim, contact_groups DL - Desktop
        host_name Server2.domain.com, contact_groups DL-Server
        host_name Server3.domain.com
        host_name Server4.domain.com, contacts Services,helpdesk,manager





        share|improve this answer

























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          I am sure you can do it much easily in awk, but awk doesn't like me much so here is my take on it using everything but the kitchen sink. Assuming that the data is in a file called file1



          export output=; while read line; do if [[ "$line" =~ "host_name" ]]; then export output="${output}n"; fi; export output="${output}, $line"; done < file1 && echo -e $output | sed 's/^, ?//' | sed '/^$/d'


          Content of file1



          host_name   Server1.domain.com
          contacts ravi, raj, rahim
          contact_groups DL - Desktop
          host_name Server2.domain.com
          contact_groups DL-Server
          host_name Server3.domain.com
          host_name Server4.domain.com
          contacts Services,helpdesk,manager


          Output of the above command



          host_name Server1.domain.com, contacts ravi, raj, rahim, contact_groups DL - Desktop
          host_name Server2.domain.com, contact_groups DL-Server
          host_name Server3.domain.com
          host_name Server4.domain.com, contacts Services,helpdesk,manager





          share|improve this answer














          I am sure you can do it much easily in awk, but awk doesn't like me much so here is my take on it using everything but the kitchen sink. Assuming that the data is in a file called file1



          export output=; while read line; do if [[ "$line" =~ "host_name" ]]; then export output="${output}n"; fi; export output="${output}, $line"; done < file1 && echo -e $output | sed 's/^, ?//' | sed '/^$/d'


          Content of file1



          host_name   Server1.domain.com
          contacts ravi, raj, rahim
          contact_groups DL - Desktop
          host_name Server2.domain.com
          contact_groups DL-Server
          host_name Server3.domain.com
          host_name Server4.domain.com
          contacts Services,helpdesk,manager


          Output of the above command



          host_name Server1.domain.com, contacts ravi, raj, rahim, contact_groups DL - Desktop
          host_name Server2.domain.com, contact_groups DL-Server
          host_name Server3.domain.com
          host_name Server4.domain.com, contacts Services,helpdesk,manager






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 8 '16 at 7:12

























          answered Jun 8 '16 at 7:07









          Hesham Ahmed

          42428




          42428
























              up vote
              1
              down vote













              $ sed -e '2,$ s/^host_name/n&/' ravi.txt | 
              perl -n -e 'if (! m/^$/) {
              chomp;
              $line .= $_ . ", "
              };

              if (m/^$/ || eof) {
              $line =~ s/ +/ /g; # merge multiple spaces into one space
              $line =~ s/, $//; # strip the trailing comma
              print $line,"n" ;
              $line=""
              }'
              host_name Server1.domain.com, contacts DL - Desktop, contact_groups ravi, raj, rahim
              host_name Server2.domain.com, contact_groups DL-Server
              host_name Server3.domain.com
              host_name Server4.domain.com, contacts Services,helpdesk,manager


              First use sed to convert the input to paragraphs (separated by a newline). Then perl to join the lines in each paragraph together and print them out.



              This could be done entirely in perl, but I was lazy and decided it was easier to just convert to paragraphs before piping into a simple perl scipt.






              share|improve this answer

























                up vote
                1
                down vote













                $ sed -e '2,$ s/^host_name/n&/' ravi.txt | 
                perl -n -e 'if (! m/^$/) {
                chomp;
                $line .= $_ . ", "
                };

                if (m/^$/ || eof) {
                $line =~ s/ +/ /g; # merge multiple spaces into one space
                $line =~ s/, $//; # strip the trailing comma
                print $line,"n" ;
                $line=""
                }'
                host_name Server1.domain.com, contacts DL - Desktop, contact_groups ravi, raj, rahim
                host_name Server2.domain.com, contact_groups DL-Server
                host_name Server3.domain.com
                host_name Server4.domain.com, contacts Services,helpdesk,manager


                First use sed to convert the input to paragraphs (separated by a newline). Then perl to join the lines in each paragraph together and print them out.



                This could be done entirely in perl, but I was lazy and decided it was easier to just convert to paragraphs before piping into a simple perl scipt.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  $ sed -e '2,$ s/^host_name/n&/' ravi.txt | 
                  perl -n -e 'if (! m/^$/) {
                  chomp;
                  $line .= $_ . ", "
                  };

                  if (m/^$/ || eof) {
                  $line =~ s/ +/ /g; # merge multiple spaces into one space
                  $line =~ s/, $//; # strip the trailing comma
                  print $line,"n" ;
                  $line=""
                  }'
                  host_name Server1.domain.com, contacts DL - Desktop, contact_groups ravi, raj, rahim
                  host_name Server2.domain.com, contact_groups DL-Server
                  host_name Server3.domain.com
                  host_name Server4.domain.com, contacts Services,helpdesk,manager


                  First use sed to convert the input to paragraphs (separated by a newline). Then perl to join the lines in each paragraph together and print them out.



                  This could be done entirely in perl, but I was lazy and decided it was easier to just convert to paragraphs before piping into a simple perl scipt.






                  share|improve this answer












                  $ sed -e '2,$ s/^host_name/n&/' ravi.txt | 
                  perl -n -e 'if (! m/^$/) {
                  chomp;
                  $line .= $_ . ", "
                  };

                  if (m/^$/ || eof) {
                  $line =~ s/ +/ /g; # merge multiple spaces into one space
                  $line =~ s/, $//; # strip the trailing comma
                  print $line,"n" ;
                  $line=""
                  }'
                  host_name Server1.domain.com, contacts DL - Desktop, contact_groups ravi, raj, rahim
                  host_name Server2.domain.com, contact_groups DL-Server
                  host_name Server3.domain.com
                  host_name Server4.domain.com, contacts Services,helpdesk,manager


                  First use sed to convert the input to paragraphs (separated by a newline). Then perl to join the lines in each paragraph together and print them out.



                  This could be done entirely in perl, but I was lazy and decided it was easier to just convert to paragraphs before piping into a simple perl scipt.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 8 '16 at 7:38









                  cas

                  38.4k44999




                  38.4k44999






























                      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%2f288324%2fdata-manipulation%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