Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum the Column based on another column

i have a file with 2 columns, 1st column is time with HH:MM:SS format and 2nd column is count.
I want to sum the 2nd column based 1st coulmn. with respect to Hour and minute of 1st column.

Input:

00:00:00,2.00,
00:00:10,1.00,
00:00:20,2.00,
00:00:30,6.00,
00:00:40,1.00,
00:00:50,7.00,
00:01:00,8.00,
00:01:10,7.00,
00:01:20,8.00,
00:01:30,11.00,

For Hour: the below awk works,

 awk -F, '{ a[substr($1,0,2)]+=$2 } END{ for (i in a) print i "," a[i] }' file

output:

00,53

For Minute:
unable sum the column based on the Minute filed, please suggesst the way to get the expected result.

Excepted Output:

00:00,19
00:01,34
like image 799
Marjer Avatar asked Feb 12 '23 13:02

Marjer


1 Answers

Using `awk:

awk -F '[:,]' -v OFS=, '{s[$1 ":" $2]+=$(NF-1)} END{for (i in s) print i, s[i]}' file
00:00,19
00:01,34

EDIT: If ordering of original timestamps is important then use:

awk -F '[:,]' -v OFS=, '{k=$1":"$2} !s[k]{b[++n]=k} {s[k]+=$(NF-1)}
     END {for (i=1; i<=n; i++) print b[i], s[b[i]]}' file
00:00,19
00:01,34
like image 57
anubhava Avatar answered Feb 14 '23 01:02

anubhava