Addition of values in second columns if the first column entry is same in UNIX
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I am trying to aggregate a file containing the following data in UNIX.
I need to add the amounts if the key is same.
Key,amount,date,Time
abc-xyz-12234,45,15-08-91,23:00
pqr-vgh-5241,15,15-08-91,21:00
abc-xyz-12234,35,15-08-91,23:00
pqr-vgh-5241,24,15-08-91,21:00
abc-xyz-12234,655,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
pqr-vgh-5241,40,15-08-91,21:00
Output should be as following
abc-xyz-12234,735,15-08-91,23:00
pqr-vgh-5241,79,15-08-91,21:00
lkj-erf-8542,281,15-08-91,10:00
I tried by the following command ,but it just gives me uniq
cat file | grep "abc-xyz-12234" | uniq
csv
add a comment |
up vote
2
down vote
favorite
I am trying to aggregate a file containing the following data in UNIX.
I need to add the amounts if the key is same.
Key,amount,date,Time
abc-xyz-12234,45,15-08-91,23:00
pqr-vgh-5241,15,15-08-91,21:00
abc-xyz-12234,35,15-08-91,23:00
pqr-vgh-5241,24,15-08-91,21:00
abc-xyz-12234,655,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
pqr-vgh-5241,40,15-08-91,21:00
Output should be as following
abc-xyz-12234,735,15-08-91,23:00
pqr-vgh-5241,79,15-08-91,21:00
lkj-erf-8542,281,15-08-91,10:00
I tried by the following command ,but it just gives me uniq
cat file | grep "abc-xyz-12234" | uniq
csv
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am trying to aggregate a file containing the following data in UNIX.
I need to add the amounts if the key is same.
Key,amount,date,Time
abc-xyz-12234,45,15-08-91,23:00
pqr-vgh-5241,15,15-08-91,21:00
abc-xyz-12234,35,15-08-91,23:00
pqr-vgh-5241,24,15-08-91,21:00
abc-xyz-12234,655,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
pqr-vgh-5241,40,15-08-91,21:00
Output should be as following
abc-xyz-12234,735,15-08-91,23:00
pqr-vgh-5241,79,15-08-91,21:00
lkj-erf-8542,281,15-08-91,10:00
I tried by the following command ,but it just gives me uniq
cat file | grep "abc-xyz-12234" | uniq
csv
I am trying to aggregate a file containing the following data in UNIX.
I need to add the amounts if the key is same.
Key,amount,date,Time
abc-xyz-12234,45,15-08-91,23:00
pqr-vgh-5241,15,15-08-91,21:00
abc-xyz-12234,35,15-08-91,23:00
pqr-vgh-5241,24,15-08-91,21:00
abc-xyz-12234,655,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
pqr-vgh-5241,40,15-08-91,21:00
Output should be as following
abc-xyz-12234,735,15-08-91,23:00
pqr-vgh-5241,79,15-08-91,21:00
lkj-erf-8542,281,15-08-91,10:00
I tried by the following command ,but it just gives me uniq
cat file | grep "abc-xyz-12234" | uniq
csv
csv
edited Nov 20 at 22:44
Rui F Ribeiro
38.2k1475125
38.2k1475125
asked Nov 6 '15 at 9:30
Kshitij
111
111
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
Another possible solution with awk
could be:
awk 'BEGIN FS = OFS = ","
NR != 1 y[$1] += $2; $2 = y[$1]; x[$1] = $0;
END for (i in x) print x[i]; ' file
The flaw is it won't preserve your order. So result could be:
pqr-vgh-5241,79,15-08-91,21:00
abc-xyz-12234,735,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
Order doesnt matters
– Kshitij
Nov 6 '15 at 10:19
add a comment |
up vote
1
down vote
You can do this with awk:
#!/bin/sh
sort | awk -F, '
function result()
if ( key != "" )
printf "%s,%d,%sn", key, value, datetime;
BEGIN key = ""; value = 0; datetime = "";
$2 ~ /^[0-9]+/
if ( $1 == key )
value += $2;
else
result();
key = $1;
value = $2;
datetime = $3 "," $4;
END result();
'
giving
./foo <input
abc-xyz-12234,735,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
pqr-vgh-5241,79,15-08-91,21:00
add a comment |
up vote
0
down vote
Here's a way in Perl. Call it as such ./script file.ext
:
use warnings;
use strict;
my %data;
my @order;
while (<>)
next if $. == 1;
my @line = split /,/;
if (defined $data$line[0])
$data$line[0]->[1] += $line[1];
else
$data$line[0] = @line;
push @order, $line[0];
for (@order)
print join(',', @$data$_);
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Another possible solution with awk
could be:
awk 'BEGIN FS = OFS = ","
NR != 1 y[$1] += $2; $2 = y[$1]; x[$1] = $0;
END for (i in x) print x[i]; ' file
The flaw is it won't preserve your order. So result could be:
pqr-vgh-5241,79,15-08-91,21:00
abc-xyz-12234,735,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
Order doesnt matters
– Kshitij
Nov 6 '15 at 10:19
add a comment |
up vote
3
down vote
Another possible solution with awk
could be:
awk 'BEGIN FS = OFS = ","
NR != 1 y[$1] += $2; $2 = y[$1]; x[$1] = $0;
END for (i in x) print x[i]; ' file
The flaw is it won't preserve your order. So result could be:
pqr-vgh-5241,79,15-08-91,21:00
abc-xyz-12234,735,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
Order doesnt matters
– Kshitij
Nov 6 '15 at 10:19
add a comment |
up vote
3
down vote
up vote
3
down vote
Another possible solution with awk
could be:
awk 'BEGIN FS = OFS = ","
NR != 1 y[$1] += $2; $2 = y[$1]; x[$1] = $0;
END for (i in x) print x[i]; ' file
The flaw is it won't preserve your order. So result could be:
pqr-vgh-5241,79,15-08-91,21:00
abc-xyz-12234,735,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
Another possible solution with awk
could be:
awk 'BEGIN FS = OFS = ","
NR != 1 y[$1] += $2; $2 = y[$1]; x[$1] = $0;
END for (i in x) print x[i]; ' file
The flaw is it won't preserve your order. So result could be:
pqr-vgh-5241,79,15-08-91,21:00
abc-xyz-12234,735,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
edited Nov 6 '15 at 10:38
cas
38.3k44898
38.3k44898
answered Nov 6 '15 at 9:48
taliezin
6,78011527
6,78011527
Order doesnt matters
– Kshitij
Nov 6 '15 at 10:19
add a comment |
Order doesnt matters
– Kshitij
Nov 6 '15 at 10:19
Order doesnt matters
– Kshitij
Nov 6 '15 at 10:19
Order doesnt matters
– Kshitij
Nov 6 '15 at 10:19
add a comment |
up vote
1
down vote
You can do this with awk:
#!/bin/sh
sort | awk -F, '
function result()
if ( key != "" )
printf "%s,%d,%sn", key, value, datetime;
BEGIN key = ""; value = 0; datetime = "";
$2 ~ /^[0-9]+/
if ( $1 == key )
value += $2;
else
result();
key = $1;
value = $2;
datetime = $3 "," $4;
END result();
'
giving
./foo <input
abc-xyz-12234,735,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
pqr-vgh-5241,79,15-08-91,21:00
add a comment |
up vote
1
down vote
You can do this with awk:
#!/bin/sh
sort | awk -F, '
function result()
if ( key != "" )
printf "%s,%d,%sn", key, value, datetime;
BEGIN key = ""; value = 0; datetime = "";
$2 ~ /^[0-9]+/
if ( $1 == key )
value += $2;
else
result();
key = $1;
value = $2;
datetime = $3 "," $4;
END result();
'
giving
./foo <input
abc-xyz-12234,735,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
pqr-vgh-5241,79,15-08-91,21:00
add a comment |
up vote
1
down vote
up vote
1
down vote
You can do this with awk:
#!/bin/sh
sort | awk -F, '
function result()
if ( key != "" )
printf "%s,%d,%sn", key, value, datetime;
BEGIN key = ""; value = 0; datetime = "";
$2 ~ /^[0-9]+/
if ( $1 == key )
value += $2;
else
result();
key = $1;
value = $2;
datetime = $3 "," $4;
END result();
'
giving
./foo <input
abc-xyz-12234,735,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
pqr-vgh-5241,79,15-08-91,21:00
You can do this with awk:
#!/bin/sh
sort | awk -F, '
function result()
if ( key != "" )
printf "%s,%d,%sn", key, value, datetime;
BEGIN key = ""; value = 0; datetime = "";
$2 ~ /^[0-9]+/
if ( $1 == key )
value += $2;
else
result();
key = $1;
value = $2;
datetime = $3 "," $4;
END result();
'
giving
./foo <input
abc-xyz-12234,735,15-08-91,23:00
lkj-erf-8542,281,15-08-91,10:00
pqr-vgh-5241,79,15-08-91,21:00
answered Nov 6 '15 at 9:46
Thomas Dickey
51.5k594164
51.5k594164
add a comment |
add a comment |
up vote
0
down vote
Here's a way in Perl. Call it as such ./script file.ext
:
use warnings;
use strict;
my %data;
my @order;
while (<>)
next if $. == 1;
my @line = split /,/;
if (defined $data$line[0])
$data$line[0]->[1] += $line[1];
else
$data$line[0] = @line;
push @order, $line[0];
for (@order)
print join(',', @$data$_);
add a comment |
up vote
0
down vote
Here's a way in Perl. Call it as such ./script file.ext
:
use warnings;
use strict;
my %data;
my @order;
while (<>)
next if $. == 1;
my @line = split /,/;
if (defined $data$line[0])
$data$line[0]->[1] += $line[1];
else
$data$line[0] = @line;
push @order, $line[0];
for (@order)
print join(',', @$data$_);
add a comment |
up vote
0
down vote
up vote
0
down vote
Here's a way in Perl. Call it as such ./script file.ext
:
use warnings;
use strict;
my %data;
my @order;
while (<>)
next if $. == 1;
my @line = split /,/;
if (defined $data$line[0])
$data$line[0]->[1] += $line[1];
else
$data$line[0] = @line;
push @order, $line[0];
for (@order)
print join(',', @$data$_);
Here's a way in Perl. Call it as such ./script file.ext
:
use warnings;
use strict;
my %data;
my @order;
while (<>)
next if $. == 1;
my @line = split /,/;
if (defined $data$line[0])
$data$line[0]->[1] += $line[1];
else
$data$line[0] = @line;
push @order, $line[0];
for (@order)
print join(',', @$data$_);
answered Nov 6 '15 at 15:14
stevieb
81647
81647
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f241192%2faddition-of-values-in-second-columns-if-the-first-column-entry-is-same-in-unix%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown