awk columns to rows break for new line












1















In my stats.txt page I have the following:



Get-VMHardDiskDrive | Select-Object Path

ComputerName : ACD-VMH04
Path : D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
VhdFormat : VHD
VhdType : Dynamic
Used Space : .07
Prov Space : 4.00

ComputerName : ACD-VMH04
Path : D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
VhdFormat : VHD
VhdType : Dynamic
Used Space : 258.04
Prov Space : 906.34


I run the following AWK command to give me this:



ACD-VMH04
D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
VHD
Dynamic
.07
4.00

ACD-VMH04
D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
VHD
Dynamic
258.04
906.34


The file has about 6 items like that however other files have more so I am wondering how to get the output to look like this:



ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34


This will somewhat do the job however I do not know how to tell it stop and make a new line when it hits a blank line.



awk -F  " : " '{ ORS = " " } /Select-Object Path/ { output = 1; start = NR + 2 }; output && NR >= start { print $2, "-" }' stats.txt









share|improve this question





























    1















    In my stats.txt page I have the following:



    Get-VMHardDiskDrive | Select-Object Path

    ComputerName : ACD-VMH04
    Path : D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
    VhdFormat : VHD
    VhdType : Dynamic
    Used Space : .07
    Prov Space : 4.00

    ComputerName : ACD-VMH04
    Path : D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
    VhdFormat : VHD
    VhdType : Dynamic
    Used Space : 258.04
    Prov Space : 906.34


    I run the following AWK command to give me this:



    ACD-VMH04
    D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
    VHD
    Dynamic
    .07
    4.00

    ACD-VMH04
    D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
    VHD
    Dynamic
    258.04
    906.34


    The file has about 6 items like that however other files have more so I am wondering how to get the output to look like this:



    ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00
    ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34


    This will somewhat do the job however I do not know how to tell it stop and make a new line when it hits a blank line.



    awk -F  " : " '{ ORS = " " } /Select-Object Path/ { output = 1; start = NR + 2 }; output && NR >= start { print $2, "-" }' stats.txt









    share|improve this question



























      1












      1








      1








      In my stats.txt page I have the following:



      Get-VMHardDiskDrive | Select-Object Path

      ComputerName : ACD-VMH04
      Path : D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
      VhdFormat : VHD
      VhdType : Dynamic
      Used Space : .07
      Prov Space : 4.00

      ComputerName : ACD-VMH04
      Path : D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
      VhdFormat : VHD
      VhdType : Dynamic
      Used Space : 258.04
      Prov Space : 906.34


      I run the following AWK command to give me this:



      ACD-VMH04
      D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
      VHD
      Dynamic
      .07
      4.00

      ACD-VMH04
      D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
      VHD
      Dynamic
      258.04
      906.34


      The file has about 6 items like that however other files have more so I am wondering how to get the output to look like this:



      ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00
      ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34


      This will somewhat do the job however I do not know how to tell it stop and make a new line when it hits a blank line.



      awk -F  " : " '{ ORS = " " } /Select-Object Path/ { output = 1; start = NR + 2 }; output && NR >= start { print $2, "-" }' stats.txt









      share|improve this question
















      In my stats.txt page I have the following:



      Get-VMHardDiskDrive | Select-Object Path

      ComputerName : ACD-VMH04
      Path : D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
      VhdFormat : VHD
      VhdType : Dynamic
      Used Space : .07
      Prov Space : 4.00

      ComputerName : ACD-VMH04
      Path : D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
      VhdFormat : VHD
      VhdType : Dynamic
      Used Space : 258.04
      Prov Space : 906.34


      I run the following AWK command to give me this:



      ACD-VMH04
      D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
      VHD
      Dynamic
      .07
      4.00

      ACD-VMH04
      D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
      VHD
      Dynamic
      258.04
      906.34


      The file has about 6 items like that however other files have more so I am wondering how to get the output to look like this:



      ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00
      ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34


      This will somewhat do the job however I do not know how to tell it stop and make a new line when it hits a blank line.



      awk -F  " : " '{ ORS = " " } /Select-Object Path/ { output = 1; start = NR + 2 }; output && NR >= start { print $2, "-" }' stats.txt






      text-processing awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 30 '17 at 21:13









      Stephen Rauch

      3,344101428




      3,344101428










      asked Mar 30 '17 at 20:02









      Anthony FornitoAnthony Fornito

      1105




      1105






















          3 Answers
          3






          active

          oldest

          votes


















          2














          I don't have a one liner for you, but the key concept here is to store the field until you have collected them all, then print them on one line. Also a blank line will have the number of fields (NF) equal to zero.



          Code:



          #!/bin/awk -f
          BEGIN {FS = " : "; OFS = " - "}
          NF > 0 {
          gsub(/[ ]+$/, "", $1)
          a[$1] = $2
          }

          NF == 0 {dump()}

          END {dump()}

          function dump() {
          print a["ComputerName"], a["Path"], a["VhdFormat"], a["VhdType"]
          }


          Results:



          ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic
          ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic





          share|improve this answer































            1














            perl -lne '
            /Select-Object Path/ and !$a++ and <>,next;

            /^ComputerNames+:/ && $a .. /^$/ || eof and do{
            push @A, (/s+:s+K(.*)/)[0] if /^ComputerNames+:/ || /./;
            print join(" - ", splice(@A,0,@A)),"n" if /^$/ || eof;
            };

            ' stats.txt


            Output



            ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00

            ACD-VMH05 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34





            share|improve this answer

































              0














              We can use " : " as delimter and print 2nd field of every line in awk




              awk -v FS=" : " -v ORS="" '{{print $2,"- "}; if ( $0 ~ /^$/) {print "n"}}' stats.txt






              share|improve this answer








              New contributor




              Deepika Reddy Billuri 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: "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%2f354922%2fawk-columns-to-rows-break-for-new-line%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














                I don't have a one liner for you, but the key concept here is to store the field until you have collected them all, then print them on one line. Also a blank line will have the number of fields (NF) equal to zero.



                Code:



                #!/bin/awk -f
                BEGIN {FS = " : "; OFS = " - "}
                NF > 0 {
                gsub(/[ ]+$/, "", $1)
                a[$1] = $2
                }

                NF == 0 {dump()}

                END {dump()}

                function dump() {
                print a["ComputerName"], a["Path"], a["VhdFormat"], a["VhdType"]
                }


                Results:



                ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic
                ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic





                share|improve this answer




























                  2














                  I don't have a one liner for you, but the key concept here is to store the field until you have collected them all, then print them on one line. Also a blank line will have the number of fields (NF) equal to zero.



                  Code:



                  #!/bin/awk -f
                  BEGIN {FS = " : "; OFS = " - "}
                  NF > 0 {
                  gsub(/[ ]+$/, "", $1)
                  a[$1] = $2
                  }

                  NF == 0 {dump()}

                  END {dump()}

                  function dump() {
                  print a["ComputerName"], a["Path"], a["VhdFormat"], a["VhdType"]
                  }


                  Results:



                  ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic
                  ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic





                  share|improve this answer


























                    2












                    2








                    2







                    I don't have a one liner for you, but the key concept here is to store the field until you have collected them all, then print them on one line. Also a blank line will have the number of fields (NF) equal to zero.



                    Code:



                    #!/bin/awk -f
                    BEGIN {FS = " : "; OFS = " - "}
                    NF > 0 {
                    gsub(/[ ]+$/, "", $1)
                    a[$1] = $2
                    }

                    NF == 0 {dump()}

                    END {dump()}

                    function dump() {
                    print a["ComputerName"], a["Path"], a["VhdFormat"], a["VhdType"]
                    }


                    Results:



                    ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic
                    ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic





                    share|improve this answer













                    I don't have a one liner for you, but the key concept here is to store the field until you have collected them all, then print them on one line. Also a blank line will have the number of fields (NF) equal to zero.



                    Code:



                    #!/bin/awk -f
                    BEGIN {FS = " : "; OFS = " - "}
                    NF > 0 {
                    gsub(/[ ]+$/, "", $1)
                    a[$1] = $2
                    }

                    NF == 0 {dump()}

                    END {dump()}

                    function dump() {
                    print a["ComputerName"], a["Path"], a["VhdFormat"], a["VhdType"]
                    }


                    Results:



                    ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic
                    ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 30 '17 at 20:43









                    Stephen RauchStephen Rauch

                    3,344101428




                    3,344101428

























                        1














                        perl -lne '
                        /Select-Object Path/ and !$a++ and <>,next;

                        /^ComputerNames+:/ && $a .. /^$/ || eof and do{
                        push @A, (/s+:s+K(.*)/)[0] if /^ComputerNames+:/ || /./;
                        print join(" - ", splice(@A,0,@A)),"n" if /^$/ || eof;
                        };

                        ' stats.txt


                        Output



                        ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00

                        ACD-VMH05 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34





                        share|improve this answer






























                          1














                          perl -lne '
                          /Select-Object Path/ and !$a++ and <>,next;

                          /^ComputerNames+:/ && $a .. /^$/ || eof and do{
                          push @A, (/s+:s+K(.*)/)[0] if /^ComputerNames+:/ || /./;
                          print join(" - ", splice(@A,0,@A)),"n" if /^$/ || eof;
                          };

                          ' stats.txt


                          Output



                          ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00

                          ACD-VMH05 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34





                          share|improve this answer




























                            1












                            1








                            1







                            perl -lne '
                            /Select-Object Path/ and !$a++ and <>,next;

                            /^ComputerNames+:/ && $a .. /^$/ || eof and do{
                            push @A, (/s+:s+K(.*)/)[0] if /^ComputerNames+:/ || /./;
                            print join(" - ", splice(@A,0,@A)),"n" if /^$/ || eof;
                            };

                            ' stats.txt


                            Output



                            ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00

                            ACD-VMH05 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34





                            share|improve this answer















                            perl -lne '
                            /Select-Object Path/ and !$a++ and <>,next;

                            /^ComputerNames+:/ && $a .. /^$/ || eof and do{
                            push @A, (/s+:s+K(.*)/)[0] if /^ComputerNames+:/ || /./;
                            print join(" - ", splice(@A,0,@A)),"n" if /^$/ || eof;
                            };

                            ' stats.txt


                            Output



                            ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00

                            ACD-VMH05 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 30 '17 at 21:58

























                            answered Mar 30 '17 at 21:36







                            user218374






























                                0














                                We can use " : " as delimter and print 2nd field of every line in awk




                                awk -v FS=" : " -v ORS="" '{{print $2,"- "}; if ( $0 ~ /^$/) {print "n"}}' stats.txt






                                share|improve this answer








                                New contributor




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

























                                  0














                                  We can use " : " as delimter and print 2nd field of every line in awk




                                  awk -v FS=" : " -v ORS="" '{{print $2,"- "}; if ( $0 ~ /^$/) {print "n"}}' stats.txt






                                  share|improve this answer








                                  New contributor




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























                                    0












                                    0








                                    0







                                    We can use " : " as delimter and print 2nd field of every line in awk




                                    awk -v FS=" : " -v ORS="" '{{print $2,"- "}; if ( $0 ~ /^$/) {print "n"}}' stats.txt






                                    share|improve this answer








                                    New contributor




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










                                    We can use " : " as delimter and print 2nd field of every line in awk




                                    awk -v FS=" : " -v ORS="" '{{print $2,"- "}; if ( $0 ~ /^$/) {print "n"}}' stats.txt







                                    share|improve this answer








                                    New contributor




                                    Deepika Reddy Billuri 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






                                    New contributor




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









                                    answered 22 mins ago









                                    Deepika Reddy BilluriDeepika Reddy Billuri

                                    12




                                    12




                                    New contributor




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





                                    New contributor





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






                                    Deepika Reddy Billuri 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 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%2f354922%2fawk-columns-to-rows-break-for-new-line%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 rewrite equation of hyperbola in standard form

                                        No ethernet ip address in my vocore2