How to remove fields with all zeros












1














I have a file that looks like this :



header,d0,d1,d2,d3, ...
s1,0,5,2,8, ...
s2,0,8,2,4, ...
s3,0,7,3,4, ...
s4,0,3,2,1, ...
...


I want to remove any column with all zeros like d0

I can manually inspect for columns with all zeros and find d0 and execute



cut -d "," -f 1,3- file> file_revised  


The desired output is



header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
...


But since I have so many columns, it is hard to inspect manually.

How can I automatically remove columns with all zeros?

Thank you.










share|improve this question
























  • Please add your desired output for that sample input to your question.
    – Cyrus
    Nov 11 at 13:47










  • And what you already tried yourself
    – Ivonet
    Nov 11 at 13:49






  • 1




    I see. I'll edit my post. Thank you!
    – Sumin Kim
    Nov 11 at 13:54
















1














I have a file that looks like this :



header,d0,d1,d2,d3, ...
s1,0,5,2,8, ...
s2,0,8,2,4, ...
s3,0,7,3,4, ...
s4,0,3,2,1, ...
...


I want to remove any column with all zeros like d0

I can manually inspect for columns with all zeros and find d0 and execute



cut -d "," -f 1,3- file> file_revised  


The desired output is



header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
...


But since I have so many columns, it is hard to inspect manually.

How can I automatically remove columns with all zeros?

Thank you.










share|improve this question
























  • Please add your desired output for that sample input to your question.
    – Cyrus
    Nov 11 at 13:47










  • And what you already tried yourself
    – Ivonet
    Nov 11 at 13:49






  • 1




    I see. I'll edit my post. Thank you!
    – Sumin Kim
    Nov 11 at 13:54














1












1








1


0





I have a file that looks like this :



header,d0,d1,d2,d3, ...
s1,0,5,2,8, ...
s2,0,8,2,4, ...
s3,0,7,3,4, ...
s4,0,3,2,1, ...
...


I want to remove any column with all zeros like d0

I can manually inspect for columns with all zeros and find d0 and execute



cut -d "," -f 1,3- file> file_revised  


The desired output is



header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
...


But since I have so many columns, it is hard to inspect manually.

How can I automatically remove columns with all zeros?

Thank you.










share|improve this question















I have a file that looks like this :



header,d0,d1,d2,d3, ...
s1,0,5,2,8, ...
s2,0,8,2,4, ...
s3,0,7,3,4, ...
s4,0,3,2,1, ...
...


I want to remove any column with all zeros like d0

I can manually inspect for columns with all zeros and find d0 and execute



cut -d "," -f 1,3- file> file_revised  


The desired output is



header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
...


But since I have so many columns, it is hard to inspect manually.

How can I automatically remove columns with all zeros?

Thank you.







awk cut






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:54

























asked Nov 11 at 13:45









Sumin Kim

1028




1028












  • Please add your desired output for that sample input to your question.
    – Cyrus
    Nov 11 at 13:47










  • And what you already tried yourself
    – Ivonet
    Nov 11 at 13:49






  • 1




    I see. I'll edit my post. Thank you!
    – Sumin Kim
    Nov 11 at 13:54


















  • Please add your desired output for that sample input to your question.
    – Cyrus
    Nov 11 at 13:47










  • And what you already tried yourself
    – Ivonet
    Nov 11 at 13:49






  • 1




    I see. I'll edit my post. Thank you!
    – Sumin Kim
    Nov 11 at 13:54
















Please add your desired output for that sample input to your question.
– Cyrus
Nov 11 at 13:47




Please add your desired output for that sample input to your question.
– Cyrus
Nov 11 at 13:47












And what you already tried yourself
– Ivonet
Nov 11 at 13:49




And what you already tried yourself
– Ivonet
Nov 11 at 13:49




1




1




I see. I'll edit my post. Thank you!
– Sumin Kim
Nov 11 at 13:54




I see. I'll edit my post. Thank you!
– Sumin Kim
Nov 11 at 13:54












5 Answers
5






active

oldest

votes


















1














$ cat file
header,d0,d1,d2,d3
s1,0,5,2,8
s2,0,8,2,4
s3,0,7,3,4
s4,0,3,2,1
$
$ cat tst.awk
NR==1 {
for (i=1; i<=NF; ++i)
a[i]
next
}
NR==FNR {
for (i in a)
if ($i != "0")
delete a[i]
next
}
{
sep = ""
out = ""
for (i=1; i<=NF; ++i) {
if (i in a)
continue
out = out sep $i
sep = FS
}
print out
}
$
$ awk -F, -f tst.awk file file
header,d1,d2,d3
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1





share|improve this answer



















  • 1




    It worked perfectly for me! Thank you very much.
    – Sumin Kim
    Nov 11 at 14:13



















2














Here is one that gathers the fields to print to a variable (p="$1,$3" ... etc.) and uses system to call awk to print p:



$ awk '
BEGIN { FS=OFS="," }
NR==1 {
for(i=1;i<=NF;i++) # gather all field numbers to c
c[i]
next }
{
for(i in c) # test all fields that still are all zeros
if($i!=0)
delete c[i] }
END { # after testing all the records
for(i=1;i<=NF;i++)
if(!(i in c))
p=p (p==""?"":OFS) "$" i # make list of list of fields to print
p="print " p # p="print $1,$3,$4,$5,$6"
system("awk 47BEGIN{FS=OFS=","}{" cmd "}47 " FILENAME)
}' file


Output:



header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...


If all fields are all zeros, p="print" and the whole file gets printed.






share|improve this answer





























    2














    Provided that the first column does not contain all zeros, this awk script should do the job



    awk -F',' '(NR==FNR && NR >1){for(i = 1; i <= NF; i++)
    {a[i] = a[i]+$i}}
    (FNR!=NR){out=$1
    for(i = 2; i<= NF; i++){
    if(a[i]!=0){out=out","$i}
    }
    print out
    }' file_name file_name


    Note that the sript takes the name of the input file file_name twice!



    For example, for the input:



    header,d0,d
    s1,0,5,2,8,
    s2,0,8,2,4,
    s3,0,7,3,4,
    s4,0,3,2,1,


    the script yields as output



    header,d
    s1,5,2,8
    s2,8,2,4
    s3,7,3,4
    s4,3,2,1





    share|improve this answer































      0














      maybe you can use sed command like below:



      $ sed 's/b0,b//g' test.txt
      header,d0,d1,d2,d3
      s1,5,2,8
      s2,8,2,4
      s3,7,3,4
      s4,3,2,1





      share|improve this answer





















      • This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
        – oguzismail
        Nov 11 at 15:05












      • yes, if zero is also in the last column, then you can trysed 's/b0,b|b,0b//g'
        – GerryLon
        Nov 12 at 4:47



















      0














      Using Perl



      > cat sumin.txt
      header,d0,d1,d2,d3
      s1,0,5,2,8
      s2,0,8,2,4
      s3,0,7,3,4
      s4,0,3,2,1
      > cat rem_zero.sh
      perl -F, -lane '
      @FH=@F if $.==1;

      if($.>1)
      {
      $F[$_] and $nz[$_]||=1 for 0..$#F;
      push(@L,[@F]);
      }

      END {
      @cols = grep $nz[$_], 0..$#nz;
      print join(",",@FH[@cols]);
      for my $line (@L) { print "@{$line}[@cols]" }
      }

      ' $1
      > rem_zero.sh sumin.txt
      header,d1,d2,d3
      s1 5 2 8
      s2 8 2 4
      s3 7 3 4
      s4 3 2 1
      >





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        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%2fstackoverflow.com%2fquestions%2f53249372%2fhow-to-remove-fields-with-all-zeros%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        $ cat file
        header,d0,d1,d2,d3
        s1,0,5,2,8
        s2,0,8,2,4
        s3,0,7,3,4
        s4,0,3,2,1
        $
        $ cat tst.awk
        NR==1 {
        for (i=1; i<=NF; ++i)
        a[i]
        next
        }
        NR==FNR {
        for (i in a)
        if ($i != "0")
        delete a[i]
        next
        }
        {
        sep = ""
        out = ""
        for (i=1; i<=NF; ++i) {
        if (i in a)
        continue
        out = out sep $i
        sep = FS
        }
        print out
        }
        $
        $ awk -F, -f tst.awk file file
        header,d1,d2,d3
        s1,5,2,8
        s2,8,2,4
        s3,7,3,4
        s4,3,2,1





        share|improve this answer



















        • 1




          It worked perfectly for me! Thank you very much.
          – Sumin Kim
          Nov 11 at 14:13
















        1














        $ cat file
        header,d0,d1,d2,d3
        s1,0,5,2,8
        s2,0,8,2,4
        s3,0,7,3,4
        s4,0,3,2,1
        $
        $ cat tst.awk
        NR==1 {
        for (i=1; i<=NF; ++i)
        a[i]
        next
        }
        NR==FNR {
        for (i in a)
        if ($i != "0")
        delete a[i]
        next
        }
        {
        sep = ""
        out = ""
        for (i=1; i<=NF; ++i) {
        if (i in a)
        continue
        out = out sep $i
        sep = FS
        }
        print out
        }
        $
        $ awk -F, -f tst.awk file file
        header,d1,d2,d3
        s1,5,2,8
        s2,8,2,4
        s3,7,3,4
        s4,3,2,1





        share|improve this answer



















        • 1




          It worked perfectly for me! Thank you very much.
          – Sumin Kim
          Nov 11 at 14:13














        1












        1








        1






        $ cat file
        header,d0,d1,d2,d3
        s1,0,5,2,8
        s2,0,8,2,4
        s3,0,7,3,4
        s4,0,3,2,1
        $
        $ cat tst.awk
        NR==1 {
        for (i=1; i<=NF; ++i)
        a[i]
        next
        }
        NR==FNR {
        for (i in a)
        if ($i != "0")
        delete a[i]
        next
        }
        {
        sep = ""
        out = ""
        for (i=1; i<=NF; ++i) {
        if (i in a)
        continue
        out = out sep $i
        sep = FS
        }
        print out
        }
        $
        $ awk -F, -f tst.awk file file
        header,d1,d2,d3
        s1,5,2,8
        s2,8,2,4
        s3,7,3,4
        s4,3,2,1





        share|improve this answer














        $ cat file
        header,d0,d1,d2,d3
        s1,0,5,2,8
        s2,0,8,2,4
        s3,0,7,3,4
        s4,0,3,2,1
        $
        $ cat tst.awk
        NR==1 {
        for (i=1; i<=NF; ++i)
        a[i]
        next
        }
        NR==FNR {
        for (i in a)
        if ($i != "0")
        delete a[i]
        next
        }
        {
        sep = ""
        out = ""
        for (i=1; i<=NF; ++i) {
        if (i in a)
        continue
        out = out sep $i
        sep = FS
        }
        print out
        }
        $
        $ awk -F, -f tst.awk file file
        header,d1,d2,d3
        s1,5,2,8
        s2,8,2,4
        s3,7,3,4
        s4,3,2,1






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 14:20

























        answered Nov 11 at 14:07









        oguzismail

        3,24131025




        3,24131025








        • 1




          It worked perfectly for me! Thank you very much.
          – Sumin Kim
          Nov 11 at 14:13














        • 1




          It worked perfectly for me! Thank you very much.
          – Sumin Kim
          Nov 11 at 14:13








        1




        1




        It worked perfectly for me! Thank you very much.
        – Sumin Kim
        Nov 11 at 14:13




        It worked perfectly for me! Thank you very much.
        – Sumin Kim
        Nov 11 at 14:13













        2














        Here is one that gathers the fields to print to a variable (p="$1,$3" ... etc.) and uses system to call awk to print p:



        $ awk '
        BEGIN { FS=OFS="," }
        NR==1 {
        for(i=1;i<=NF;i++) # gather all field numbers to c
        c[i]
        next }
        {
        for(i in c) # test all fields that still are all zeros
        if($i!=0)
        delete c[i] }
        END { # after testing all the records
        for(i=1;i<=NF;i++)
        if(!(i in c))
        p=p (p==""?"":OFS) "$" i # make list of list of fields to print
        p="print " p # p="print $1,$3,$4,$5,$6"
        system("awk 47BEGIN{FS=OFS=","}{" cmd "}47 " FILENAME)
        }' file


        Output:



        header,d1,d2,d3, ...
        s1,5,2,8, ...
        s2,8,2,4, ...
        s3,7,3,4, ...
        s4,3,2,1, ...


        If all fields are all zeros, p="print" and the whole file gets printed.






        share|improve this answer


























          2














          Here is one that gathers the fields to print to a variable (p="$1,$3" ... etc.) and uses system to call awk to print p:



          $ awk '
          BEGIN { FS=OFS="," }
          NR==1 {
          for(i=1;i<=NF;i++) # gather all field numbers to c
          c[i]
          next }
          {
          for(i in c) # test all fields that still are all zeros
          if($i!=0)
          delete c[i] }
          END { # after testing all the records
          for(i=1;i<=NF;i++)
          if(!(i in c))
          p=p (p==""?"":OFS) "$" i # make list of list of fields to print
          p="print " p # p="print $1,$3,$4,$5,$6"
          system("awk 47BEGIN{FS=OFS=","}{" cmd "}47 " FILENAME)
          }' file


          Output:



          header,d1,d2,d3, ...
          s1,5,2,8, ...
          s2,8,2,4, ...
          s3,7,3,4, ...
          s4,3,2,1, ...


          If all fields are all zeros, p="print" and the whole file gets printed.






          share|improve this answer
























            2












            2








            2






            Here is one that gathers the fields to print to a variable (p="$1,$3" ... etc.) and uses system to call awk to print p:



            $ awk '
            BEGIN { FS=OFS="," }
            NR==1 {
            for(i=1;i<=NF;i++) # gather all field numbers to c
            c[i]
            next }
            {
            for(i in c) # test all fields that still are all zeros
            if($i!=0)
            delete c[i] }
            END { # after testing all the records
            for(i=1;i<=NF;i++)
            if(!(i in c))
            p=p (p==""?"":OFS) "$" i # make list of list of fields to print
            p="print " p # p="print $1,$3,$4,$5,$6"
            system("awk 47BEGIN{FS=OFS=","}{" cmd "}47 " FILENAME)
            }' file


            Output:



            header,d1,d2,d3, ...
            s1,5,2,8, ...
            s2,8,2,4, ...
            s3,7,3,4, ...
            s4,3,2,1, ...


            If all fields are all zeros, p="print" and the whole file gets printed.






            share|improve this answer












            Here is one that gathers the fields to print to a variable (p="$1,$3" ... etc.) and uses system to call awk to print p:



            $ awk '
            BEGIN { FS=OFS="," }
            NR==1 {
            for(i=1;i<=NF;i++) # gather all field numbers to c
            c[i]
            next }
            {
            for(i in c) # test all fields that still are all zeros
            if($i!=0)
            delete c[i] }
            END { # after testing all the records
            for(i=1;i<=NF;i++)
            if(!(i in c))
            p=p (p==""?"":OFS) "$" i # make list of list of fields to print
            p="print " p # p="print $1,$3,$4,$5,$6"
            system("awk 47BEGIN{FS=OFS=","}{" cmd "}47 " FILENAME)
            }' file


            Output:



            header,d1,d2,d3, ...
            s1,5,2,8, ...
            s2,8,2,4, ...
            s3,7,3,4, ...
            s4,3,2,1, ...


            If all fields are all zeros, p="print" and the whole file gets printed.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 16:10









            James Brown

            18k31635




            18k31635























                2














                Provided that the first column does not contain all zeros, this awk script should do the job



                awk -F',' '(NR==FNR && NR >1){for(i = 1; i <= NF; i++)
                {a[i] = a[i]+$i}}
                (FNR!=NR){out=$1
                for(i = 2; i<= NF; i++){
                if(a[i]!=0){out=out","$i}
                }
                print out
                }' file_name file_name


                Note that the sript takes the name of the input file file_name twice!



                For example, for the input:



                header,d0,d
                s1,0,5,2,8,
                s2,0,8,2,4,
                s3,0,7,3,4,
                s4,0,3,2,1,


                the script yields as output



                header,d
                s1,5,2,8
                s2,8,2,4
                s3,7,3,4
                s4,3,2,1





                share|improve this answer




























                  2














                  Provided that the first column does not contain all zeros, this awk script should do the job



                  awk -F',' '(NR==FNR && NR >1){for(i = 1; i <= NF; i++)
                  {a[i] = a[i]+$i}}
                  (FNR!=NR){out=$1
                  for(i = 2; i<= NF; i++){
                  if(a[i]!=0){out=out","$i}
                  }
                  print out
                  }' file_name file_name


                  Note that the sript takes the name of the input file file_name twice!



                  For example, for the input:



                  header,d0,d
                  s1,0,5,2,8,
                  s2,0,8,2,4,
                  s3,0,7,3,4,
                  s4,0,3,2,1,


                  the script yields as output



                  header,d
                  s1,5,2,8
                  s2,8,2,4
                  s3,7,3,4
                  s4,3,2,1





                  share|improve this answer


























                    2












                    2








                    2






                    Provided that the first column does not contain all zeros, this awk script should do the job



                    awk -F',' '(NR==FNR && NR >1){for(i = 1; i <= NF; i++)
                    {a[i] = a[i]+$i}}
                    (FNR!=NR){out=$1
                    for(i = 2; i<= NF; i++){
                    if(a[i]!=0){out=out","$i}
                    }
                    print out
                    }' file_name file_name


                    Note that the sript takes the name of the input file file_name twice!



                    For example, for the input:



                    header,d0,d
                    s1,0,5,2,8,
                    s2,0,8,2,4,
                    s3,0,7,3,4,
                    s4,0,3,2,1,


                    the script yields as output



                    header,d
                    s1,5,2,8
                    s2,8,2,4
                    s3,7,3,4
                    s4,3,2,1





                    share|improve this answer














                    Provided that the first column does not contain all zeros, this awk script should do the job



                    awk -F',' '(NR==FNR && NR >1){for(i = 1; i <= NF; i++)
                    {a[i] = a[i]+$i}}
                    (FNR!=NR){out=$1
                    for(i = 2; i<= NF; i++){
                    if(a[i]!=0){out=out","$i}
                    }
                    print out
                    }' file_name file_name


                    Note that the sript takes the name of the input file file_name twice!



                    For example, for the input:



                    header,d0,d
                    s1,0,5,2,8,
                    s2,0,8,2,4,
                    s3,0,7,3,4,
                    s4,0,3,2,1,


                    the script yields as output



                    header,d
                    s1,5,2,8
                    s2,8,2,4
                    s3,7,3,4
                    s4,3,2,1






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 16 at 7:16

























                    answered Nov 11 at 14:17









                    F. Knorr

                    2,357716




                    2,357716























                        0














                        maybe you can use sed command like below:



                        $ sed 's/b0,b//g' test.txt
                        header,d0,d1,d2,d3
                        s1,5,2,8
                        s2,8,2,4
                        s3,7,3,4
                        s4,3,2,1





                        share|improve this answer





















                        • This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
                          – oguzismail
                          Nov 11 at 15:05












                        • yes, if zero is also in the last column, then you can trysed 's/b0,b|b,0b//g'
                          – GerryLon
                          Nov 12 at 4:47
















                        0














                        maybe you can use sed command like below:



                        $ sed 's/b0,b//g' test.txt
                        header,d0,d1,d2,d3
                        s1,5,2,8
                        s2,8,2,4
                        s3,7,3,4
                        s4,3,2,1





                        share|improve this answer





















                        • This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
                          – oguzismail
                          Nov 11 at 15:05












                        • yes, if zero is also in the last column, then you can trysed 's/b0,b|b,0b//g'
                          – GerryLon
                          Nov 12 at 4:47














                        0












                        0








                        0






                        maybe you can use sed command like below:



                        $ sed 's/b0,b//g' test.txt
                        header,d0,d1,d2,d3
                        s1,5,2,8
                        s2,8,2,4
                        s3,7,3,4
                        s4,3,2,1





                        share|improve this answer












                        maybe you can use sed command like below:



                        $ sed 's/b0,b//g' test.txt
                        header,d0,d1,d2,d3
                        s1,5,2,8
                        s2,8,2,4
                        s3,7,3,4
                        s4,3,2,1






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 11 at 14:48









                        GerryLon

                        444




                        444












                        • This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
                          – oguzismail
                          Nov 11 at 15:05












                        • yes, if zero is also in the last column, then you can trysed 's/b0,b|b,0b//g'
                          – GerryLon
                          Nov 12 at 4:47


















                        • This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
                          – oguzismail
                          Nov 11 at 15:05












                        • yes, if zero is also in the last column, then you can trysed 's/b0,b|b,0b//g'
                          – GerryLon
                          Nov 12 at 4:47
















                        This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
                        – oguzismail
                        Nov 11 at 15:05






                        This will remove every cell containing a zero, except the ones in the last column. OP wants to delete columns that containing all zeros
                        – oguzismail
                        Nov 11 at 15:05














                        yes, if zero is also in the last column, then you can trysed 's/b0,b|b,0b//g'
                        – GerryLon
                        Nov 12 at 4:47




                        yes, if zero is also in the last column, then you can trysed 's/b0,b|b,0b//g'
                        – GerryLon
                        Nov 12 at 4:47











                        0














                        Using Perl



                        > cat sumin.txt
                        header,d0,d1,d2,d3
                        s1,0,5,2,8
                        s2,0,8,2,4
                        s3,0,7,3,4
                        s4,0,3,2,1
                        > cat rem_zero.sh
                        perl -F, -lane '
                        @FH=@F if $.==1;

                        if($.>1)
                        {
                        $F[$_] and $nz[$_]||=1 for 0..$#F;
                        push(@L,[@F]);
                        }

                        END {
                        @cols = grep $nz[$_], 0..$#nz;
                        print join(",",@FH[@cols]);
                        for my $line (@L) { print "@{$line}[@cols]" }
                        }

                        ' $1
                        > rem_zero.sh sumin.txt
                        header,d1,d2,d3
                        s1 5 2 8
                        s2 8 2 4
                        s3 7 3 4
                        s4 3 2 1
                        >





                        share|improve this answer


























                          0














                          Using Perl



                          > cat sumin.txt
                          header,d0,d1,d2,d3
                          s1,0,5,2,8
                          s2,0,8,2,4
                          s3,0,7,3,4
                          s4,0,3,2,1
                          > cat rem_zero.sh
                          perl -F, -lane '
                          @FH=@F if $.==1;

                          if($.>1)
                          {
                          $F[$_] and $nz[$_]||=1 for 0..$#F;
                          push(@L,[@F]);
                          }

                          END {
                          @cols = grep $nz[$_], 0..$#nz;
                          print join(",",@FH[@cols]);
                          for my $line (@L) { print "@{$line}[@cols]" }
                          }

                          ' $1
                          > rem_zero.sh sumin.txt
                          header,d1,d2,d3
                          s1 5 2 8
                          s2 8 2 4
                          s3 7 3 4
                          s4 3 2 1
                          >





                          share|improve this answer
























                            0












                            0








                            0






                            Using Perl



                            > cat sumin.txt
                            header,d0,d1,d2,d3
                            s1,0,5,2,8
                            s2,0,8,2,4
                            s3,0,7,3,4
                            s4,0,3,2,1
                            > cat rem_zero.sh
                            perl -F, -lane '
                            @FH=@F if $.==1;

                            if($.>1)
                            {
                            $F[$_] and $nz[$_]||=1 for 0..$#F;
                            push(@L,[@F]);
                            }

                            END {
                            @cols = grep $nz[$_], 0..$#nz;
                            print join(",",@FH[@cols]);
                            for my $line (@L) { print "@{$line}[@cols]" }
                            }

                            ' $1
                            > rem_zero.sh sumin.txt
                            header,d1,d2,d3
                            s1 5 2 8
                            s2 8 2 4
                            s3 7 3 4
                            s4 3 2 1
                            >





                            share|improve this answer












                            Using Perl



                            > cat sumin.txt
                            header,d0,d1,d2,d3
                            s1,0,5,2,8
                            s2,0,8,2,4
                            s3,0,7,3,4
                            s4,0,3,2,1
                            > cat rem_zero.sh
                            perl -F, -lane '
                            @FH=@F if $.==1;

                            if($.>1)
                            {
                            $F[$_] and $nz[$_]||=1 for 0..$#F;
                            push(@L,[@F]);
                            }

                            END {
                            @cols = grep $nz[$_], 0..$#nz;
                            print join(",",@FH[@cols]);
                            for my $line (@L) { print "@{$line}[@cols]" }
                            }

                            ' $1
                            > rem_zero.sh sumin.txt
                            header,d1,d2,d3
                            s1 5 2 8
                            s2 8 2 4
                            s3 7 3 4
                            s4 3 2 1
                            >






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 17 at 19:59









                            stack0114106

                            1,9851416




                            1,9851416






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • 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%2fstackoverflow.com%2fquestions%2f53249372%2fhow-to-remove-fields-with-all-zeros%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







                                這個網誌中的熱門文章

                                Xamarin.form Move up view when keyboard appear

                                Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                Anylogic : not able to use stopDelay()