Subtotal of rows [closed]
Clash 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
text-processing
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.
add a comment |Â
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
text-processing
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
add a comment |Â
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
text-processing
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
text-processing
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
add a comment |Â
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
add a comment |Â
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
Thanks Kusalananda!
â HandymanAlan
Sep 26 at 5:36
add a comment |Â
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
Thanks Kusalananda!
â HandymanAlan
Sep 26 at 5:36
add a comment |Â
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
Thanks Kusalananda!
â HandymanAlan
Sep 26 at 5:36
add a comment |Â
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
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
answered Sep 25 at 9:14
Kusalananda
108k14209332
108k14209332
Thanks Kusalananda!
â HandymanAlan
Sep 26 at 5:36
add a comment |Â
Thanks Kusalananda!
â HandymanAlan
Sep 26 at 5:36
Thanks Kusalananda!
â HandymanAlan
Sep 26 at 5:36
Thanks Kusalananda!
â HandymanAlan
Sep 26 at 5:36
add a comment |Â
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