Sum values in 5th column that correspond to same field in 2nd column

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












Considering below file:



0,2,,,10
0,2,,,15
0,1,,,984
0,2,,,9
1,14,,,5


Using awk, I need to calculate the total value in $5 per each $2.



The desired output would look like below:



2,34
1,984
14,5






share|improve this question


























    up vote
    2
    down vote

    favorite












    Considering below file:



    0,2,,,10
    0,2,,,15
    0,1,,,984
    0,2,,,9
    1,14,,,5


    Using awk, I need to calculate the total value in $5 per each $2.



    The desired output would look like below:



    2,34
    1,984
    14,5






    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Considering below file:



      0,2,,,10
      0,2,,,15
      0,1,,,984
      0,2,,,9
      1,14,,,5


      Using awk, I need to calculate the total value in $5 per each $2.



      The desired output would look like below:



      2,34
      1,984
      14,5






      share|improve this question














      Considering below file:



      0,2,,,10
      0,2,,,15
      0,1,,,984
      0,2,,,9
      1,14,,,5


      Using awk, I need to calculate the total value in $5 per each $2.



      The desired output would look like below:



      2,34
      1,984
      14,5








      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 26 '15 at 13:01









      don_crissti

      46.4k15123153




      46.4k15123153










      asked Aug 9 '15 at 13:21









      Eng7

      8312721




      8312721




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Try:



          awk -F, 'a[$2]+=$5;ENDfor(i in a)print i","a[i]' <file


          A note that array traversal in POSIX awk is unspecified order.






          share|improve this answer





























            up vote
            2
            down vote













            With gnu datamash:



            datamash -t ',' -s -g 2 sum 5 <infile


            the output will be sorted by 2nd column:



            1,984
            14,5
            2,34





            share|improve this answer





























              up vote
              1
              down vote













              I'd be tempted to use perl:



              #!/usr/bin/env perl
              use strict;
              use warnings;

              my %things;

              while (<>)
              my ( undef, $key, @rest ) = split(/,/);
              $things$key += pop(@rest);


              foreach my $key ( sort $a <=> $b keys %things )
              print "$key = $things$keyn";



              You could condense that down to a one liner if needs be.






              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: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                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%2f222128%2fsum-values-in-5th-column-that-correspond-to-same-field-in-2nd-column%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                4
                down vote



                accepted










                Try:



                awk -F, 'a[$2]+=$5;ENDfor(i in a)print i","a[i]' <file


                A note that array traversal in POSIX awk is unspecified order.






                share|improve this answer


























                  up vote
                  4
                  down vote



                  accepted










                  Try:



                  awk -F, 'a[$2]+=$5;ENDfor(i in a)print i","a[i]' <file


                  A note that array traversal in POSIX awk is unspecified order.






                  share|improve this answer
























                    up vote
                    4
                    down vote



                    accepted







                    up vote
                    4
                    down vote



                    accepted






                    Try:



                    awk -F, 'a[$2]+=$5;ENDfor(i in a)print i","a[i]' <file


                    A note that array traversal in POSIX awk is unspecified order.






                    share|improve this answer














                    Try:



                    awk -F, 'a[$2]+=$5;ENDfor(i in a)print i","a[i]' <file


                    A note that array traversal in POSIX awk is unspecified order.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Apr 13 '17 at 12:36









                    Community♦

                    1




                    1










                    answered Aug 9 '15 at 14:00









                    cuonglm

                    97.3k21185278




                    97.3k21185278






















                        up vote
                        2
                        down vote













                        With gnu datamash:



                        datamash -t ',' -s -g 2 sum 5 <infile


                        the output will be sorted by 2nd column:



                        1,984
                        14,5
                        2,34





                        share|improve this answer


























                          up vote
                          2
                          down vote













                          With gnu datamash:



                          datamash -t ',' -s -g 2 sum 5 <infile


                          the output will be sorted by 2nd column:



                          1,984
                          14,5
                          2,34





                          share|improve this answer
























                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            With gnu datamash:



                            datamash -t ',' -s -g 2 sum 5 <infile


                            the output will be sorted by 2nd column:



                            1,984
                            14,5
                            2,34





                            share|improve this answer














                            With gnu datamash:



                            datamash -t ',' -s -g 2 sum 5 <infile


                            the output will be sorted by 2nd column:



                            1,984
                            14,5
                            2,34






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            answered Sep 26 '15 at 13:03


























                            community wiki





                            don_crissti





















                                up vote
                                1
                                down vote













                                I'd be tempted to use perl:



                                #!/usr/bin/env perl
                                use strict;
                                use warnings;

                                my %things;

                                while (<>)
                                my ( undef, $key, @rest ) = split(/,/);
                                $things$key += pop(@rest);


                                foreach my $key ( sort $a <=> $b keys %things )
                                print "$key = $things$keyn";



                                You could condense that down to a one liner if needs be.






                                share|improve this answer
























                                  up vote
                                  1
                                  down vote













                                  I'd be tempted to use perl:



                                  #!/usr/bin/env perl
                                  use strict;
                                  use warnings;

                                  my %things;

                                  while (<>)
                                  my ( undef, $key, @rest ) = split(/,/);
                                  $things$key += pop(@rest);


                                  foreach my $key ( sort $a <=> $b keys %things )
                                  print "$key = $things$keyn";



                                  You could condense that down to a one liner if needs be.






                                  share|improve this answer






















                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    down vote









                                    I'd be tempted to use perl:



                                    #!/usr/bin/env perl
                                    use strict;
                                    use warnings;

                                    my %things;

                                    while (<>)
                                    my ( undef, $key, @rest ) = split(/,/);
                                    $things$key += pop(@rest);


                                    foreach my $key ( sort $a <=> $b keys %things )
                                    print "$key = $things$keyn";



                                    You could condense that down to a one liner if needs be.






                                    share|improve this answer












                                    I'd be tempted to use perl:



                                    #!/usr/bin/env perl
                                    use strict;
                                    use warnings;

                                    my %things;

                                    while (<>)
                                    my ( undef, $key, @rest ) = split(/,/);
                                    $things$key += pop(@rest);


                                    foreach my $key ( sort $a <=> $b keys %things )
                                    print "$key = $things$keyn";



                                    You could condense that down to a one liner if needs be.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Aug 9 '15 at 15:43









                                    Sobrique

                                    3,729517




                                    3,729517






















                                         

                                        draft saved


                                        draft discarded


























                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f222128%2fsum-values-in-5th-column-that-correspond-to-same-field-in-2nd-column%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Popular posts from this blog

                                        How to check contact read email or not when send email to Individual?

                                        Bahrain

                                        Postfix configuration issue with fips on centos 7; mailgun relay