Addition of values in second columns if the first column entry is same in UNIX

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











up vote
2
down vote

favorite












I am trying to aggregate a file containing the following data in UNIX.
I need to add the amounts if the key is same.



Key,amount,date,Time
abc-xyz-12234,45,15-08-91,23:00
pqr-vgh-5241,15,15-08-91,21:00
abc-xyz-12234,35,15-08-91,23:00
pqr-vgh-5241,24,15-08-91,21:00
abc-xyz-12234,655,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
pqr-vgh-5241,40,15-08-91,21:00


Output should be as following



abc-xyz-12234,735,15-08-91,23:00
pqr-vgh-5241,79,15-08-91,21:00
lkj-erf-8542,281,15-08-91,10:00


I tried by the following command ,but it just gives me uniq



cat file | grep "abc-xyz-12234" | uniq









share|improve this question



























    up vote
    2
    down vote

    favorite












    I am trying to aggregate a file containing the following data in UNIX.
    I need to add the amounts if the key is same.



    Key,amount,date,Time
    abc-xyz-12234,45,15-08-91,23:00
    pqr-vgh-5241,15,15-08-91,21:00
    abc-xyz-12234,35,15-08-91,23:00
    pqr-vgh-5241,24,15-08-91,21:00
    abc-xyz-12234,655,15-08-91,23:00
    lkj-erf-8542,281,15-08-91,10:00
    pqr-vgh-5241,40,15-08-91,21:00


    Output should be as following



    abc-xyz-12234,735,15-08-91,23:00
    pqr-vgh-5241,79,15-08-91,21:00
    lkj-erf-8542,281,15-08-91,10:00


    I tried by the following command ,but it just gives me uniq



    cat file | grep "abc-xyz-12234" | uniq









    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I am trying to aggregate a file containing the following data in UNIX.
      I need to add the amounts if the key is same.



      Key,amount,date,Time
      abc-xyz-12234,45,15-08-91,23:00
      pqr-vgh-5241,15,15-08-91,21:00
      abc-xyz-12234,35,15-08-91,23:00
      pqr-vgh-5241,24,15-08-91,21:00
      abc-xyz-12234,655,15-08-91,23:00
      lkj-erf-8542,281,15-08-91,10:00
      pqr-vgh-5241,40,15-08-91,21:00


      Output should be as following



      abc-xyz-12234,735,15-08-91,23:00
      pqr-vgh-5241,79,15-08-91,21:00
      lkj-erf-8542,281,15-08-91,10:00


      I tried by the following command ,but it just gives me uniq



      cat file | grep "abc-xyz-12234" | uniq









      share|improve this question















      I am trying to aggregate a file containing the following data in UNIX.
      I need to add the amounts if the key is same.



      Key,amount,date,Time
      abc-xyz-12234,45,15-08-91,23:00
      pqr-vgh-5241,15,15-08-91,21:00
      abc-xyz-12234,35,15-08-91,23:00
      pqr-vgh-5241,24,15-08-91,21:00
      abc-xyz-12234,655,15-08-91,23:00
      lkj-erf-8542,281,15-08-91,10:00
      pqr-vgh-5241,40,15-08-91,21:00


      Output should be as following



      abc-xyz-12234,735,15-08-91,23:00
      pqr-vgh-5241,79,15-08-91,21:00
      lkj-erf-8542,281,15-08-91,10:00


      I tried by the following command ,but it just gives me uniq



      cat file | grep "abc-xyz-12234" | uniq






      csv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 22:44









      Rui F Ribeiro

      38.2k1475125




      38.2k1475125










      asked Nov 6 '15 at 9:30









      Kshitij

      111




      111




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote













          Another possible solution with awk could be:



          awk 'BEGIN FS = OFS = "," 
          NR != 1 y[$1] += $2; $2 = y[$1]; x[$1] = $0;
          END for (i in x) print x[i]; ' file


          The flaw is it won't preserve your order. So result could be:



          pqr-vgh-5241,79,15-08-91,21:00
          abc-xyz-12234,735,15-08-91,23:00
          lkj-erf-8542,281,15-08-91,10:00





          share|improve this answer






















          • Order doesnt matters
            – Kshitij
            Nov 6 '15 at 10:19

















          up vote
          1
          down vote













          You can do this with awk:



          #!/bin/sh
          sort | awk -F, '
          function result()
          if ( key != "" )
          printf "%s,%d,%sn", key, value, datetime;


          BEGIN key = ""; value = 0; datetime = "";
          $2 ~ /^[0-9]+/
          if ( $1 == key )
          value += $2;
          else
          result();
          key = $1;
          value = $2;
          datetime = $3 "," $4;


          END result();
          '


          giving



          ./foo <input
          abc-xyz-12234,735,15-08-91,23:00
          lkj-erf-8542,281,15-08-91,10:00
          pqr-vgh-5241,79,15-08-91,21:00





          share|improve this answer



























            up vote
            0
            down vote













            Here's a way in Perl. Call it as such ./script file.ext:



            use warnings;
            use strict;

            my %data;
            my @order;

            while (<>)
            next if $. == 1;
            my @line = split /,/;
            if (defined $data$line[0])
            $data$line[0]->[1] += $line[1];

            else
            $data$line[0] = @line;
            push @order, $line[0];



            for (@order)
            print join(',', @$data$_);






            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%2f241192%2faddition-of-values-in-second-columns-if-the-first-column-entry-is-same-in-unix%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








              up vote
              3
              down vote













              Another possible solution with awk could be:



              awk 'BEGIN FS = OFS = "," 
              NR != 1 y[$1] += $2; $2 = y[$1]; x[$1] = $0;
              END for (i in x) print x[i]; ' file


              The flaw is it won't preserve your order. So result could be:



              pqr-vgh-5241,79,15-08-91,21:00
              abc-xyz-12234,735,15-08-91,23:00
              lkj-erf-8542,281,15-08-91,10:00





              share|improve this answer






















              • Order doesnt matters
                – Kshitij
                Nov 6 '15 at 10:19














              up vote
              3
              down vote













              Another possible solution with awk could be:



              awk 'BEGIN FS = OFS = "," 
              NR != 1 y[$1] += $2; $2 = y[$1]; x[$1] = $0;
              END for (i in x) print x[i]; ' file


              The flaw is it won't preserve your order. So result could be:



              pqr-vgh-5241,79,15-08-91,21:00
              abc-xyz-12234,735,15-08-91,23:00
              lkj-erf-8542,281,15-08-91,10:00





              share|improve this answer






















              • Order doesnt matters
                – Kshitij
                Nov 6 '15 at 10:19












              up vote
              3
              down vote










              up vote
              3
              down vote









              Another possible solution with awk could be:



              awk 'BEGIN FS = OFS = "," 
              NR != 1 y[$1] += $2; $2 = y[$1]; x[$1] = $0;
              END for (i in x) print x[i]; ' file


              The flaw is it won't preserve your order. So result could be:



              pqr-vgh-5241,79,15-08-91,21:00
              abc-xyz-12234,735,15-08-91,23:00
              lkj-erf-8542,281,15-08-91,10:00





              share|improve this answer














              Another possible solution with awk could be:



              awk 'BEGIN FS = OFS = "," 
              NR != 1 y[$1] += $2; $2 = y[$1]; x[$1] = $0;
              END for (i in x) print x[i]; ' file


              The flaw is it won't preserve your order. So result could be:



              pqr-vgh-5241,79,15-08-91,21:00
              abc-xyz-12234,735,15-08-91,23:00
              lkj-erf-8542,281,15-08-91,10:00






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 6 '15 at 10:38









              cas

              38.3k44898




              38.3k44898










              answered Nov 6 '15 at 9:48









              taliezin

              6,78011527




              6,78011527











              • Order doesnt matters
                – Kshitij
                Nov 6 '15 at 10:19
















              • Order doesnt matters
                – Kshitij
                Nov 6 '15 at 10:19















              Order doesnt matters
              – Kshitij
              Nov 6 '15 at 10:19




              Order doesnt matters
              – Kshitij
              Nov 6 '15 at 10:19












              up vote
              1
              down vote













              You can do this with awk:



              #!/bin/sh
              sort | awk -F, '
              function result()
              if ( key != "" )
              printf "%s,%d,%sn", key, value, datetime;


              BEGIN key = ""; value = 0; datetime = "";
              $2 ~ /^[0-9]+/
              if ( $1 == key )
              value += $2;
              else
              result();
              key = $1;
              value = $2;
              datetime = $3 "," $4;


              END result();
              '


              giving



              ./foo <input
              abc-xyz-12234,735,15-08-91,23:00
              lkj-erf-8542,281,15-08-91,10:00
              pqr-vgh-5241,79,15-08-91,21:00





              share|improve this answer
























                up vote
                1
                down vote













                You can do this with awk:



                #!/bin/sh
                sort | awk -F, '
                function result()
                if ( key != "" )
                printf "%s,%d,%sn", key, value, datetime;


                BEGIN key = ""; value = 0; datetime = "";
                $2 ~ /^[0-9]+/
                if ( $1 == key )
                value += $2;
                else
                result();
                key = $1;
                value = $2;
                datetime = $3 "," $4;


                END result();
                '


                giving



                ./foo <input
                abc-xyz-12234,735,15-08-91,23:00
                lkj-erf-8542,281,15-08-91,10:00
                pqr-vgh-5241,79,15-08-91,21:00





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You can do this with awk:



                  #!/bin/sh
                  sort | awk -F, '
                  function result()
                  if ( key != "" )
                  printf "%s,%d,%sn", key, value, datetime;


                  BEGIN key = ""; value = 0; datetime = "";
                  $2 ~ /^[0-9]+/
                  if ( $1 == key )
                  value += $2;
                  else
                  result();
                  key = $1;
                  value = $2;
                  datetime = $3 "," $4;


                  END result();
                  '


                  giving



                  ./foo <input
                  abc-xyz-12234,735,15-08-91,23:00
                  lkj-erf-8542,281,15-08-91,10:00
                  pqr-vgh-5241,79,15-08-91,21:00





                  share|improve this answer












                  You can do this with awk:



                  #!/bin/sh
                  sort | awk -F, '
                  function result()
                  if ( key != "" )
                  printf "%s,%d,%sn", key, value, datetime;


                  BEGIN key = ""; value = 0; datetime = "";
                  $2 ~ /^[0-9]+/
                  if ( $1 == key )
                  value += $2;
                  else
                  result();
                  key = $1;
                  value = $2;
                  datetime = $3 "," $4;


                  END result();
                  '


                  giving



                  ./foo <input
                  abc-xyz-12234,735,15-08-91,23:00
                  lkj-erf-8542,281,15-08-91,10:00
                  pqr-vgh-5241,79,15-08-91,21:00






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 6 '15 at 9:46









                  Thomas Dickey

                  51.5k594164




                  51.5k594164




















                      up vote
                      0
                      down vote













                      Here's a way in Perl. Call it as such ./script file.ext:



                      use warnings;
                      use strict;

                      my %data;
                      my @order;

                      while (<>)
                      next if $. == 1;
                      my @line = split /,/;
                      if (defined $data$line[0])
                      $data$line[0]->[1] += $line[1];

                      else
                      $data$line[0] = @line;
                      push @order, $line[0];



                      for (@order)
                      print join(',', @$data$_);






                      share|improve this answer
























                        up vote
                        0
                        down vote













                        Here's a way in Perl. Call it as such ./script file.ext:



                        use warnings;
                        use strict;

                        my %data;
                        my @order;

                        while (<>)
                        next if $. == 1;
                        my @line = split /,/;
                        if (defined $data$line[0])
                        $data$line[0]->[1] += $line[1];

                        else
                        $data$line[0] = @line;
                        push @order, $line[0];



                        for (@order)
                        print join(',', @$data$_);






                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Here's a way in Perl. Call it as such ./script file.ext:



                          use warnings;
                          use strict;

                          my %data;
                          my @order;

                          while (<>)
                          next if $. == 1;
                          my @line = split /,/;
                          if (defined $data$line[0])
                          $data$line[0]->[1] += $line[1];

                          else
                          $data$line[0] = @line;
                          push @order, $line[0];



                          for (@order)
                          print join(',', @$data$_);






                          share|improve this answer












                          Here's a way in Perl. Call it as such ./script file.ext:



                          use warnings;
                          use strict;

                          my %data;
                          my @order;

                          while (<>)
                          next if $. == 1;
                          my @line = split /,/;
                          if (defined $data$line[0])
                          $data$line[0]->[1] += $line[1];

                          else
                          $data$line[0] = @line;
                          push @order, $line[0];



                          for (@order)
                          print join(',', @$data$_);







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 6 '15 at 15:14









                          stevieb

                          81647




                          81647



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f241192%2faddition-of-values-in-second-columns-if-the-first-column-entry-is-same-in-unix%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 check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay