AWK: Sum of values in a column based on entries in another column [duplicate]

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











up vote
7
down vote

favorite
4













This question already has an answer here:



  • Using awk to sum the values of a column, based on the values of another column

    4 answers



  • Need to sum of last column based on pipe delimiter

    4 answers



I would like to sum numerical values listed in the third column of the table below based on entries were given in the first column of the same table. The table content is as follows:



John|Login|2
Mary|Login|10
Mary|Payroll|100
John|Login|200
John|Logout|10
Mary|Payroll|10


The expected outcome is as follows:



John|Login|202
John|Logout|10
Mary|Login|10
Mary|Payroll|110


How can I use awk to get this output?










share|improve this question















marked as duplicate by Jeff Schaller, Goro, αғsнιη, Romeo Ninov, RalfFriedl Sep 25 at 16:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Very similar to unix.stackexchange.com/questions/471251/subtotal-of-rows and other similar questions.
    – Kusalananda
    Sep 25 at 13:21










  • Certainly, awk can do math. That's not your question, though. What's your question? What is the operation?
    – Jeff Schaller
    Sep 25 at 13:23










  • Wanted to sum-up the values in the 3rd column based on the values in 1st and 2nd column.
    – user279255
    Sep 25 at 13:24










  • Please edit your question so that it's all together; thank you!
    – Jeff Schaller
    Sep 25 at 13:30














up vote
7
down vote

favorite
4













This question already has an answer here:



  • Using awk to sum the values of a column, based on the values of another column

    4 answers



  • Need to sum of last column based on pipe delimiter

    4 answers



I would like to sum numerical values listed in the third column of the table below based on entries were given in the first column of the same table. The table content is as follows:



John|Login|2
Mary|Login|10
Mary|Payroll|100
John|Login|200
John|Logout|10
Mary|Payroll|10


The expected outcome is as follows:



John|Login|202
John|Logout|10
Mary|Login|10
Mary|Payroll|110


How can I use awk to get this output?










share|improve this question















marked as duplicate by Jeff Schaller, Goro, αғsнιη, Romeo Ninov, RalfFriedl Sep 25 at 16:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Very similar to unix.stackexchange.com/questions/471251/subtotal-of-rows and other similar questions.
    – Kusalananda
    Sep 25 at 13:21










  • Certainly, awk can do math. That's not your question, though. What's your question? What is the operation?
    – Jeff Schaller
    Sep 25 at 13:23










  • Wanted to sum-up the values in the 3rd column based on the values in 1st and 2nd column.
    – user279255
    Sep 25 at 13:24










  • Please edit your question so that it's all together; thank you!
    – Jeff Schaller
    Sep 25 at 13:30












up vote
7
down vote

favorite
4









up vote
7
down vote

favorite
4






4






This question already has an answer here:



  • Using awk to sum the values of a column, based on the values of another column

    4 answers



  • Need to sum of last column based on pipe delimiter

    4 answers



I would like to sum numerical values listed in the third column of the table below based on entries were given in the first column of the same table. The table content is as follows:



John|Login|2
Mary|Login|10
Mary|Payroll|100
John|Login|200
John|Logout|10
Mary|Payroll|10


The expected outcome is as follows:



John|Login|202
John|Logout|10
Mary|Login|10
Mary|Payroll|110


How can I use awk to get this output?










share|improve this question
















This question already has an answer here:



  • Using awk to sum the values of a column, based on the values of another column

    4 answers



  • Need to sum of last column based on pipe delimiter

    4 answers



I would like to sum numerical values listed in the third column of the table below based on entries were given in the first column of the same table. The table content is as follows:



John|Login|2
Mary|Login|10
Mary|Payroll|100
John|Login|200
John|Logout|10
Mary|Payroll|10


The expected outcome is as follows:



John|Login|202
John|Logout|10
Mary|Login|10
Mary|Payroll|110


How can I use awk to get this output?





This question already has an answer here:



  • Using awk to sum the values of a column, based on the values of another column

    4 answers



  • Need to sum of last column based on pipe delimiter

    4 answers







shell-script awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 25 at 13:59









Goro

6,42552863




6,42552863










asked Sep 25 at 13:17









user279255

525




525




marked as duplicate by Jeff Schaller, Goro, αғsнιη, Romeo Ninov, RalfFriedl Sep 25 at 16:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Jeff Schaller, Goro, αғsнιη, Romeo Ninov, RalfFriedl Sep 25 at 16:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • Very similar to unix.stackexchange.com/questions/471251/subtotal-of-rows and other similar questions.
    – Kusalananda
    Sep 25 at 13:21










  • Certainly, awk can do math. That's not your question, though. What's your question? What is the operation?
    – Jeff Schaller
    Sep 25 at 13:23










  • Wanted to sum-up the values in the 3rd column based on the values in 1st and 2nd column.
    – user279255
    Sep 25 at 13:24










  • Please edit your question so that it's all together; thank you!
    – Jeff Schaller
    Sep 25 at 13:30
















  • Very similar to unix.stackexchange.com/questions/471251/subtotal-of-rows and other similar questions.
    – Kusalananda
    Sep 25 at 13:21










  • Certainly, awk can do math. That's not your question, though. What's your question? What is the operation?
    – Jeff Schaller
    Sep 25 at 13:23










  • Wanted to sum-up the values in the 3rd column based on the values in 1st and 2nd column.
    – user279255
    Sep 25 at 13:24










  • Please edit your question so that it's all together; thank you!
    – Jeff Schaller
    Sep 25 at 13:30















Very similar to unix.stackexchange.com/questions/471251/subtotal-of-rows and other similar questions.
– Kusalananda
Sep 25 at 13:21




Very similar to unix.stackexchange.com/questions/471251/subtotal-of-rows and other similar questions.
– Kusalananda
Sep 25 at 13:21












Certainly, awk can do math. That's not your question, though. What's your question? What is the operation?
– Jeff Schaller
Sep 25 at 13:23




Certainly, awk can do math. That's not your question, though. What's your question? What is the operation?
– Jeff Schaller
Sep 25 at 13:23












Wanted to sum-up the values in the 3rd column based on the values in 1st and 2nd column.
– user279255
Sep 25 at 13:24




Wanted to sum-up the values in the 3rd column based on the values in 1st and 2nd column.
– user279255
Sep 25 at 13:24












Please edit your question so that it's all together; thank you!
– Jeff Schaller
Sep 25 at 13:30




Please edit your question so that it's all together; thank you!
– Jeff Schaller
Sep 25 at 13:30










2 Answers
2






active

oldest

votes

















up vote
5
down vote



accepted










This command worked for me. it should work for you :



 awk -F '|' '"]+= $3 ENDfor (i in a) print i, a[i]' filename | sort -k 1,1
John|Login| 202
John|Logout| 10
Mary|Login| 10
Mary|Payroll| 110





share|improve this answer





























    up vote
    1
    down vote













    If you have GNU awk installed, you don't have to use another tool sort to set the order right, but you can do it in awk itself. You can set the way the array traversal is done by setting a special variable PROCINFO["sorted_in"]. See Using Predefined Array Scanning Orders with gawk



    In your case though, you can set it to sort by the index in ascending order



    awk -vFS="|" -vOFS="|" ' 
    primaryKey=($1 FS $2)

    db[primaryKey]+=$3; next
    END
    PROCINFO["sorted_in"] = "@ind_str_asc"
    for(key in db)
    print key, db[key]
    ' file





    share|improve this answer



























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      5
      down vote



      accepted










      This command worked for me. it should work for you :



       awk -F '|' '"]+= $3 ENDfor (i in a) print i, a[i]' filename | sort -k 1,1
      John|Login| 202
      John|Logout| 10
      Mary|Login| 10
      Mary|Payroll| 110





      share|improve this answer


























        up vote
        5
        down vote



        accepted










        This command worked for me. it should work for you :



         awk -F '|' '"]+= $3 ENDfor (i in a) print i, a[i]' filename | sort -k 1,1
        John|Login| 202
        John|Logout| 10
        Mary|Login| 10
        Mary|Payroll| 110





        share|improve this answer
























          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          This command worked for me. it should work for you :



           awk -F '|' '"]+= $3 ENDfor (i in a) print i, a[i]' filename | sort -k 1,1
          John|Login| 202
          John|Logout| 10
          Mary|Login| 10
          Mary|Payroll| 110





          share|improve this answer














          This command worked for me. it should work for you :



           awk -F '|' '"]+= $3 ENDfor (i in a) print i, a[i]' filename | sort -k 1,1
          John|Login| 202
          John|Logout| 10
          Mary|Login| 10
          Mary|Payroll| 110






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 25 at 14:10









          GAD3R

          23.2k164896




          23.2k164896










          answered Sep 25 at 13:50









          Shervan

          1839




          1839






















              up vote
              1
              down vote













              If you have GNU awk installed, you don't have to use another tool sort to set the order right, but you can do it in awk itself. You can set the way the array traversal is done by setting a special variable PROCINFO["sorted_in"]. See Using Predefined Array Scanning Orders with gawk



              In your case though, you can set it to sort by the index in ascending order



              awk -vFS="|" -vOFS="|" ' 
              primaryKey=($1 FS $2)

              db[primaryKey]+=$3; next
              END
              PROCINFO["sorted_in"] = "@ind_str_asc"
              for(key in db)
              print key, db[key]
              ' file





              share|improve this answer
























                up vote
                1
                down vote













                If you have GNU awk installed, you don't have to use another tool sort to set the order right, but you can do it in awk itself. You can set the way the array traversal is done by setting a special variable PROCINFO["sorted_in"]. See Using Predefined Array Scanning Orders with gawk



                In your case though, you can set it to sort by the index in ascending order



                awk -vFS="|" -vOFS="|" ' 
                primaryKey=($1 FS $2)

                db[primaryKey]+=$3; next
                END
                PROCINFO["sorted_in"] = "@ind_str_asc"
                for(key in db)
                print key, db[key]
                ' file





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  If you have GNU awk installed, you don't have to use another tool sort to set the order right, but you can do it in awk itself. You can set the way the array traversal is done by setting a special variable PROCINFO["sorted_in"]. See Using Predefined Array Scanning Orders with gawk



                  In your case though, you can set it to sort by the index in ascending order



                  awk -vFS="|" -vOFS="|" ' 
                  primaryKey=($1 FS $2)

                  db[primaryKey]+=$3; next
                  END
                  PROCINFO["sorted_in"] = "@ind_str_asc"
                  for(key in db)
                  print key, db[key]
                  ' file





                  share|improve this answer












                  If you have GNU awk installed, you don't have to use another tool sort to set the order right, but you can do it in awk itself. You can set the way the array traversal is done by setting a special variable PROCINFO["sorted_in"]. See Using Predefined Array Scanning Orders with gawk



                  In your case though, you can set it to sort by the index in ascending order



                  awk -vFS="|" -vOFS="|" ' 
                  primaryKey=($1 FS $2)

                  db[primaryKey]+=$3; next
                  END
                  PROCINFO["sorted_in"] = "@ind_str_asc"
                  for(key in db)
                  print key, db[key]
                  ' file






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 25 at 14:09









                  Inian

                  2,900822




                  2,900822












                      Popular posts from this blog

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

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?