Subtotal of rows [closed]

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











up vote
-3
down vote

favorite












I am new using linux and I have a file like this:



Category Abundance 
A0001 167
A0001 5673
A0001 32
A0002 436
A0002 6885
A0003 432


And it goes on to half a million. Is there a way I could add up the total for each category in linux? Thanks!



Cheers



Alan










share|improve this question















closed as unclear what you're asking by jasonwryan, andcoz, Goro, sebasth, Romeo Ninov Sep 25 at 9:59


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    Welcome to Unix Stackexchange! You can take the tour first and then learn How to Ask a good question. That makes it easier for us to help you.
    – andcoz
    Sep 25 at 8:09










  • This is not a script service. Please, edit your question and show us what you tried so far.
    – andcoz
    Sep 25 at 8:10






  • 1




    Possible duplicate of awk - Group by and sum column values
    – sebasth
    Sep 25 at 9:09














up vote
-3
down vote

favorite












I am new using linux and I have a file like this:



Category Abundance 
A0001 167
A0001 5673
A0001 32
A0002 436
A0002 6885
A0003 432


And it goes on to half a million. Is there a way I could add up the total for each category in linux? Thanks!



Cheers



Alan










share|improve this question















closed as unclear what you're asking by jasonwryan, andcoz, Goro, sebasth, Romeo Ninov Sep 25 at 9:59


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    Welcome to Unix Stackexchange! You can take the tour first and then learn How to Ask a good question. That makes it easier for us to help you.
    – andcoz
    Sep 25 at 8:09










  • This is not a script service. Please, edit your question and show us what you tried so far.
    – andcoz
    Sep 25 at 8:10






  • 1




    Possible duplicate of awk - Group by and sum column values
    – sebasth
    Sep 25 at 9:09












up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











I am new using linux and I have a file like this:



Category Abundance 
A0001 167
A0001 5673
A0001 32
A0002 436
A0002 6885
A0003 432


And it goes on to half a million. Is there a way I could add up the total for each category in linux? Thanks!



Cheers



Alan










share|improve this question















I am new using linux and I have a file like this:



Category Abundance 
A0001 167
A0001 5673
A0001 32
A0002 436
A0002 6885
A0003 432


And it goes on to half a million. Is there a way I could add up the total for each category in linux? Thanks!



Cheers



Alan







text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 25 at 9:18









Kusalananda

108k14209332




108k14209332










asked Sep 25 at 7:44









HandymanAlan

1




1




closed as unclear what you're asking by jasonwryan, andcoz, Goro, sebasth, Romeo Ninov Sep 25 at 9:59


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by jasonwryan, andcoz, Goro, sebasth, Romeo Ninov Sep 25 at 9:59


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    Welcome to Unix Stackexchange! You can take the tour first and then learn How to Ask a good question. That makes it easier for us to help you.
    – andcoz
    Sep 25 at 8:09










  • This is not a script service. Please, edit your question and show us what you tried so far.
    – andcoz
    Sep 25 at 8:10






  • 1




    Possible duplicate of awk - Group by and sum column values
    – sebasth
    Sep 25 at 9:09












  • 1




    Welcome to Unix Stackexchange! You can take the tour first and then learn How to Ask a good question. That makes it easier for us to help you.
    – andcoz
    Sep 25 at 8:09










  • This is not a script service. Please, edit your question and show us what you tried so far.
    – andcoz
    Sep 25 at 8:10






  • 1




    Possible duplicate of awk - Group by and sum column values
    – sebasth
    Sep 25 at 9:09







1




1




Welcome to Unix Stackexchange! You can take the tour first and then learn How to Ask a good question. That makes it easier for us to help you.
– andcoz
Sep 25 at 8:09




Welcome to Unix Stackexchange! You can take the tour first and then learn How to Ask a good question. That makes it easier for us to help you.
– andcoz
Sep 25 at 8:09












This is not a script service. Please, edit your question and show us what you tried so far.
– andcoz
Sep 25 at 8:10




This is not a script service. Please, edit your question and show us what you tried so far.
– andcoz
Sep 25 at 8:10




1




1




Possible duplicate of awk - Group by and sum column values
– sebasth
Sep 25 at 9:09




Possible duplicate of awk - Group by and sum column values
– sebasth
Sep 25 at 9:09










1 Answer
1






active

oldest

votes

















up vote
1
down vote













Assuming the data is already sorted on the category:



awk '
NR == 1 print; next
cat != $1 if (cat != "") print cat, sum; cat = $1; sum = 0
sum += $2
END print cat, sum ' file


The first block simply outputs the same header line as is present in the input, and then skips to the next line of input.



The second block triggers when we spot a new category (or the first category). It prints the category name and the accumulated sum and then resets the category name and the sum.



The second to last block unconditionally updates the sum.



The END block outputs the information for the very last category.



Output when running on the supplied data:



Category Abundance
A0001 5872
A0002 7321
A0003 432





share|improve this answer




















  • Thanks Kusalananda!
    – HandymanAlan
    Sep 26 at 5:36

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Assuming the data is already sorted on the category:



awk '
NR == 1 print; next
cat != $1 if (cat != "") print cat, sum; cat = $1; sum = 0
sum += $2
END print cat, sum ' file


The first block simply outputs the same header line as is present in the input, and then skips to the next line of input.



The second block triggers when we spot a new category (or the first category). It prints the category name and the accumulated sum and then resets the category name and the sum.



The second to last block unconditionally updates the sum.



The END block outputs the information for the very last category.



Output when running on the supplied data:



Category Abundance
A0001 5872
A0002 7321
A0003 432





share|improve this answer




















  • Thanks Kusalananda!
    – HandymanAlan
    Sep 26 at 5:36














up vote
1
down vote













Assuming the data is already sorted on the category:



awk '
NR == 1 print; next
cat != $1 if (cat != "") print cat, sum; cat = $1; sum = 0
sum += $2
END print cat, sum ' file


The first block simply outputs the same header line as is present in the input, and then skips to the next line of input.



The second block triggers when we spot a new category (or the first category). It prints the category name and the accumulated sum and then resets the category name and the sum.



The second to last block unconditionally updates the sum.



The END block outputs the information for the very last category.



Output when running on the supplied data:



Category Abundance
A0001 5872
A0002 7321
A0003 432





share|improve this answer




















  • Thanks Kusalananda!
    – HandymanAlan
    Sep 26 at 5:36












up vote
1
down vote










up vote
1
down vote









Assuming the data is already sorted on the category:



awk '
NR == 1 print; next
cat != $1 if (cat != "") print cat, sum; cat = $1; sum = 0
sum += $2
END print cat, sum ' file


The first block simply outputs the same header line as is present in the input, and then skips to the next line of input.



The second block triggers when we spot a new category (or the first category). It prints the category name and the accumulated sum and then resets the category name and the sum.



The second to last block unconditionally updates the sum.



The END block outputs the information for the very last category.



Output when running on the supplied data:



Category Abundance
A0001 5872
A0002 7321
A0003 432





share|improve this answer












Assuming the data is already sorted on the category:



awk '
NR == 1 print; next
cat != $1 if (cat != "") print cat, sum; cat = $1; sum = 0
sum += $2
END print cat, sum ' file


The first block simply outputs the same header line as is present in the input, and then skips to the next line of input.



The second block triggers when we spot a new category (or the first category). It prints the category name and the accumulated sum and then resets the category name and the sum.



The second to last block unconditionally updates the sum.



The END block outputs the information for the very last category.



Output when running on the supplied data:



Category Abundance
A0001 5872
A0002 7321
A0003 432






share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 25 at 9:14









Kusalananda

108k14209332




108k14209332











  • Thanks Kusalananda!
    – HandymanAlan
    Sep 26 at 5:36
















  • Thanks Kusalananda!
    – HandymanAlan
    Sep 26 at 5:36















Thanks Kusalananda!
– HandymanAlan
Sep 26 at 5:36




Thanks Kusalananda!
– HandymanAlan
Sep 26 at 5:36


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