Find all occurrences and add them together [duplicate]

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
This question already has an answer here:
Summing values if same second column value
4 answers
I have a file like this:
user1, 10
user2, 5
user2, 6
user1, 15
user3, 23
user1, 15
I'd like to have totals by column 1 (user1, user2, user3), and make it look like this:
user1, 40
user2, 11
user3, 23
What would be the best way forward? A while loop on the first column and sum up the 2nd column as long as it's the same?
bash shell-script csv
marked as duplicate by Kusalananda
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 7 at 9:45
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.
add a comment |Â
up vote
0
down vote
favorite
This question already has an answer here:
Summing values if same second column value
4 answers
I have a file like this:
user1, 10
user2, 5
user2, 6
user1, 15
user3, 23
user1, 15
I'd like to have totals by column 1 (user1, user2, user3), and make it look like this:
user1, 40
user2, 11
user3, 23
What would be the best way forward? A while loop on the first column and sum up the 2nd column as long as it's the same?
bash shell-script csv
marked as duplicate by Kusalananda
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 7 at 9:45
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.
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
Summing values if same second column value
4 answers
I have a file like this:
user1, 10
user2, 5
user2, 6
user1, 15
user3, 23
user1, 15
I'd like to have totals by column 1 (user1, user2, user3), and make it look like this:
user1, 40
user2, 11
user3, 23
What would be the best way forward? A while loop on the first column and sum up the 2nd column as long as it's the same?
bash shell-script csv
This question already has an answer here:
Summing values if same second column value
4 answers
I have a file like this:
user1, 10
user2, 5
user2, 6
user1, 15
user3, 23
user1, 15
I'd like to have totals by column 1 (user1, user2, user3), and make it look like this:
user1, 40
user2, 11
user3, 23
What would be the best way forward? A while loop on the first column and sum up the 2nd column as long as it's the same?
This question already has an answer here:
Summing values if same second column value
4 answers
bash shell-script csv
asked Mar 7 at 9:31
Tuinslak
1032
1032
marked as duplicate by Kusalananda
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 7 at 9:45
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 Kusalananda
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 7 at 9:45
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.
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
One-line awk:
$ awk -F, 'a[$1] += $2 END for (x in a) printf "%s, %sn", x, a[x] ' < data
user1, 40
user2, 11
user3, 23
That's rather straightforward, the field separator is set to the comma with -F, (this, and the comma in the output are about the only changes needed from the duplicate). $1 and $2 are the first and second fields, and since awk has associative arrays, collecting the sums is simple. You may need to sort the output afterward if that matters.
Sure, we could do this purely in Bash/ksh/zsh too, since it also supports associative arrays, but it would be uglier, slower and more prone to accidents with funny values. And only useful on a system that for some reason has a big shell, but no awk. (see edit history if you really want that...)
Both are validbashcode, but as you say, the second one is not the right way to process text in shells.
â Stéphane Chazelas
Mar 7 at 9:48
Note that the order will not be deterministic.
â Stéphane Chazelas
Mar 7 at 9:48
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
One-line awk:
$ awk -F, 'a[$1] += $2 END for (x in a) printf "%s, %sn", x, a[x] ' < data
user1, 40
user2, 11
user3, 23
That's rather straightforward, the field separator is set to the comma with -F, (this, and the comma in the output are about the only changes needed from the duplicate). $1 and $2 are the first and second fields, and since awk has associative arrays, collecting the sums is simple. You may need to sort the output afterward if that matters.
Sure, we could do this purely in Bash/ksh/zsh too, since it also supports associative arrays, but it would be uglier, slower and more prone to accidents with funny values. And only useful on a system that for some reason has a big shell, but no awk. (see edit history if you really want that...)
Both are validbashcode, but as you say, the second one is not the right way to process text in shells.
â Stéphane Chazelas
Mar 7 at 9:48
Note that the order will not be deterministic.
â Stéphane Chazelas
Mar 7 at 9:48
add a comment |Â
up vote
2
down vote
accepted
One-line awk:
$ awk -F, 'a[$1] += $2 END for (x in a) printf "%s, %sn", x, a[x] ' < data
user1, 40
user2, 11
user3, 23
That's rather straightforward, the field separator is set to the comma with -F, (this, and the comma in the output are about the only changes needed from the duplicate). $1 and $2 are the first and second fields, and since awk has associative arrays, collecting the sums is simple. You may need to sort the output afterward if that matters.
Sure, we could do this purely in Bash/ksh/zsh too, since it also supports associative arrays, but it would be uglier, slower and more prone to accidents with funny values. And only useful on a system that for some reason has a big shell, but no awk. (see edit history if you really want that...)
Both are validbashcode, but as you say, the second one is not the right way to process text in shells.
â Stéphane Chazelas
Mar 7 at 9:48
Note that the order will not be deterministic.
â Stéphane Chazelas
Mar 7 at 9:48
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
One-line awk:
$ awk -F, 'a[$1] += $2 END for (x in a) printf "%s, %sn", x, a[x] ' < data
user1, 40
user2, 11
user3, 23
That's rather straightforward, the field separator is set to the comma with -F, (this, and the comma in the output are about the only changes needed from the duplicate). $1 and $2 are the first and second fields, and since awk has associative arrays, collecting the sums is simple. You may need to sort the output afterward if that matters.
Sure, we could do this purely in Bash/ksh/zsh too, since it also supports associative arrays, but it would be uglier, slower and more prone to accidents with funny values. And only useful on a system that for some reason has a big shell, but no awk. (see edit history if you really want that...)
One-line awk:
$ awk -F, 'a[$1] += $2 END for (x in a) printf "%s, %sn", x, a[x] ' < data
user1, 40
user2, 11
user3, 23
That's rather straightforward, the field separator is set to the comma with -F, (this, and the comma in the output are about the only changes needed from the duplicate). $1 and $2 are the first and second fields, and since awk has associative arrays, collecting the sums is simple. You may need to sort the output afterward if that matters.
Sure, we could do this purely in Bash/ksh/zsh too, since it also supports associative arrays, but it would be uglier, slower and more prone to accidents with funny values. And only useful on a system that for some reason has a big shell, but no awk. (see edit history if you really want that...)
edited Mar 7 at 9:54
answered Mar 7 at 9:36
ilkkachu
49.2k672136
49.2k672136
Both are validbashcode, but as you say, the second one is not the right way to process text in shells.
â Stéphane Chazelas
Mar 7 at 9:48
Note that the order will not be deterministic.
â Stéphane Chazelas
Mar 7 at 9:48
add a comment |Â
Both are validbashcode, but as you say, the second one is not the right way to process text in shells.
â Stéphane Chazelas
Mar 7 at 9:48
Note that the order will not be deterministic.
â Stéphane Chazelas
Mar 7 at 9:48
Both are valid
bash code, but as you say, the second one is not the right way to process text in shells.â Stéphane Chazelas
Mar 7 at 9:48
Both are valid
bash code, but as you say, the second one is not the right way to process text in shells.â Stéphane Chazelas
Mar 7 at 9:48
Note that the order will not be deterministic.
â Stéphane Chazelas
Mar 7 at 9:48
Note that the order will not be deterministic.
â Stéphane Chazelas
Mar 7 at 9:48
add a comment |Â