Sum values in 5th column that correspond to same field in 2nd column
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Considering below file:
0,2,,,10
0,2,,,15
0,1,,,984
0,2,,,9
1,14,,,5
Using awk
, I need to calculate the total value in $5
per each $2
.
The desired output would look like below:
2,34
1,984
14,5
text-processing awk
add a comment |Â
up vote
2
down vote
favorite
Considering below file:
0,2,,,10
0,2,,,15
0,1,,,984
0,2,,,9
1,14,,,5
Using awk
, I need to calculate the total value in $5
per each $2
.
The desired output would look like below:
2,34
1,984
14,5
text-processing awk
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Considering below file:
0,2,,,10
0,2,,,15
0,1,,,984
0,2,,,9
1,14,,,5
Using awk
, I need to calculate the total value in $5
per each $2
.
The desired output would look like below:
2,34
1,984
14,5
text-processing awk
Considering below file:
0,2,,,10
0,2,,,15
0,1,,,984
0,2,,,9
1,14,,,5
Using awk
, I need to calculate the total value in $5
per each $2
.
The desired output would look like below:
2,34
1,984
14,5
text-processing awk
edited Sep 26 '15 at 13:01
don_crissti
46.4k15123153
46.4k15123153
asked Aug 9 '15 at 13:21
Eng7
8312721
8312721
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
Try:
awk -F, 'a[$2]+=$5;ENDfor(i in a)print i","a[i]' <file
A note that array traversal in POSIX awk is unspecified order.
add a comment |Â
up vote
2
down vote
With gnu
datamash
:
datamash -t ',' -s -g 2 sum 5 <infile
the output will be sorted by 2nd column:
1,984
14,5
2,34
add a comment |Â
up vote
1
down vote
I'd be tempted to use perl:
#!/usr/bin/env perl
use strict;
use warnings;
my %things;
while (<>)
my ( undef, $key, @rest ) = split(/,/);
$things$key += pop(@rest);
foreach my $key ( sort $a <=> $b keys %things )
print "$key = $things$keyn";
You could condense that down to a one liner if needs be.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Try:
awk -F, 'a[$2]+=$5;ENDfor(i in a)print i","a[i]' <file
A note that array traversal in POSIX awk is unspecified order.
add a comment |Â
up vote
4
down vote
accepted
Try:
awk -F, 'a[$2]+=$5;ENDfor(i in a)print i","a[i]' <file
A note that array traversal in POSIX awk is unspecified order.
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Try:
awk -F, 'a[$2]+=$5;ENDfor(i in a)print i","a[i]' <file
A note that array traversal in POSIX awk is unspecified order.
Try:
awk -F, 'a[$2]+=$5;ENDfor(i in a)print i","a[i]' <file
A note that array traversal in POSIX awk is unspecified order.
edited Apr 13 '17 at 12:36
Communityâ¦
1
1
answered Aug 9 '15 at 14:00
cuonglm
97.3k21185278
97.3k21185278
add a comment |Â
add a comment |Â
up vote
2
down vote
With gnu
datamash
:
datamash -t ',' -s -g 2 sum 5 <infile
the output will be sorted by 2nd column:
1,984
14,5
2,34
add a comment |Â
up vote
2
down vote
With gnu
datamash
:
datamash -t ',' -s -g 2 sum 5 <infile
the output will be sorted by 2nd column:
1,984
14,5
2,34
add a comment |Â
up vote
2
down vote
up vote
2
down vote
With gnu
datamash
:
datamash -t ',' -s -g 2 sum 5 <infile
the output will be sorted by 2nd column:
1,984
14,5
2,34
With gnu
datamash
:
datamash -t ',' -s -g 2 sum 5 <infile
the output will be sorted by 2nd column:
1,984
14,5
2,34
answered Sep 26 '15 at 13:03
community wiki
don_crissti
add a comment |Â
add a comment |Â
up vote
1
down vote
I'd be tempted to use perl:
#!/usr/bin/env perl
use strict;
use warnings;
my %things;
while (<>)
my ( undef, $key, @rest ) = split(/,/);
$things$key += pop(@rest);
foreach my $key ( sort $a <=> $b keys %things )
print "$key = $things$keyn";
You could condense that down to a one liner if needs be.
add a comment |Â
up vote
1
down vote
I'd be tempted to use perl:
#!/usr/bin/env perl
use strict;
use warnings;
my %things;
while (<>)
my ( undef, $key, @rest ) = split(/,/);
$things$key += pop(@rest);
foreach my $key ( sort $a <=> $b keys %things )
print "$key = $things$keyn";
You could condense that down to a one liner if needs be.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I'd be tempted to use perl:
#!/usr/bin/env perl
use strict;
use warnings;
my %things;
while (<>)
my ( undef, $key, @rest ) = split(/,/);
$things$key += pop(@rest);
foreach my $key ( sort $a <=> $b keys %things )
print "$key = $things$keyn";
You could condense that down to a one liner if needs be.
I'd be tempted to use perl:
#!/usr/bin/env perl
use strict;
use warnings;
my %things;
while (<>)
my ( undef, $key, @rest ) = split(/,/);
$things$key += pop(@rest);
foreach my $key ( sort $a <=> $b keys %things )
print "$key = $things$keyn";
You could condense that down to a one liner if needs be.
answered Aug 9 '15 at 15:43
Sobrique
3,729517
3,729517
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f222128%2fsum-values-in-5th-column-that-correspond-to-same-field-in-2nd-column%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password