Want to add those that have the same minute in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to add those that have the same minute in a file
# 1  
Old 01-03-2016
Want to add those that have the same minute in a file

Hi All,

Need your help on how i can sum up the values. I have a file that contains the count and the time. I wanted to add up all the first column having the same datestamp. Please see below.

INPUT
Code:
1721    2015-12-26 00:01
1440    2015-12-26 00:02
1477    2015-12-26 00:02
411     2015-12-26 00:02
1204    2015-12-26 00:06
4413    2015-12-26 00:07
1389    2015-12-26 00:08
2013    2015-12-26 00:08
1441    2015-12-26 00:11
1439    2015-12-26 00:12
1310    2015-12-26 00:13
1650    2015-12-26 00:13
2275    2015-12-26 00:16
1549    2015-12-26 00:17
1469    2015-12-26 00:17
1289    2015-12-26 00:18
8504    2015-12-26 00:21
1357    2015-12-26 00:22
1346    2015-12-26 00:22
1283    2015-12-26 00:22
40      2015-12-26 00:22
2672    2015-12-26 00:26
1537    2015-12-26 00:27
3121    2015-12-26 00:27
4355    2015-12-26 00:28
93      2015-12-26 00:28
2300    2015-12-26 00:31
1378    2015-12-26 00:32
4953    2015-12-26 00:32
2742    2015-12-26 00:33
460     2015-12-26 00:33
1784    2015-12-26 00:36
2636    2015-12-26 00:37
2254    2015-12-26 00:37
56      2015-12-26 00:37
5624    2015-12-26 00:42
3356    2015-12-26 00:42
2230    2015-12-26 00:42
686     2015-12-26 00:43
5012    2015-12-26 00:46
1423    2015-12-26 00:47
1063    2015-12-26 00:47
716     2015-12-26 00:47
2869    2015-12-26 00:51
1474    2015-12-26 00:52
1408    2015-12-26 00:52
4587    2015-12-26 00:52
1660    2015-12-26 00:57
3223    2015-12-26 00:57
1149    2015-12-26 00:58
1912    2015-12-26 00:58
3467    2015-12-26 01:01
4683    2015-12-26 01:02
1197    2015-12-26 01:02
1296    2015-12-26 01:02
581     2015-12-26 01:06
1635    2015-12-26 01:07
3245    2015-12-26 01:07
4879    2015-12-26 01:08
1578    2015-12-26 01:12
1678    2015-12-26 01:12
1495    2015-12-26 01:13
1415    2015-12-26 01:13
2134    2015-12-26 01:16
1663    2015-12-26 01:17
1338    2015-12-26 01:17
674     2015-12-26 01:18
2066    2015-12-26 01:21

OUPUT
Code:
1721	2015-12-26 00:01
3328	2015-12-26 00:02
1204	2015-12-26 00:06
4413	2015-12-26 00:07
3402	2015-12-26 00:08
1441	2015-12-26 00:11
1439	2015-12-26 00:12
2960	2015-12-26 00:13
2275	2015-12-26 00:16
3018	2015-12-26 00:17
1289	2015-12-26 00:18
8504	2015-12-26 00:21
4026	2015-12-26 00:22
2672	2015-12-26 00:26
4658	2015-12-26 00:27
4448	2015-12-26 00:28
2300	2015-12-26 00:31
6331	2015-12-26 00:32
3202	2015-12-26 00:33
1784	2015-12-26 00:36
4946	2015-12-26 00:37
11210	2015-12-26 00:42
686	2015-12-26 00:43
5012	2015-12-26 00:46
3202	2015-12-26 00:47
2869	2015-12-26 00:51
7469	2015-12-26 00:52
4883	2015-12-26 00:57
3061	2015-12-26 00:58
3467	2015-12-26 01:01
7176	2015-12-26 01:02
581	2015-12-26 01:06
4880	2015-12-26 01:07
4879	2015-12-26 01:08
3256	2015-12-26 01:12
2910	2015-12-26 01:13
2134	2015-12-26 01:16
3001	2015-12-26 01:17
674	2015-12-26 01:18
2066	2015-12-26 01:21

Please help.

Thanks,
Ernie

Last edited by rbatte1; 01-04-2016 at 01:08 PM.. Reason: Took BOLD off input
# 2  
Old 01-04-2016
Please, try:
Code:
awk '{a[$2 FS $3] += $1} END{for (i in a){print a[i] OFS  i}}' OFS="\t" ernesto.input | sort -k2 > ernesto.output


Last edited by Aia; 01-04-2016 at 12:36 AM..
# 3  
Old 01-04-2016
Code:
awk '{ x[$2" "$3] += $1 } END { for (i in x) {printf("%s\t%s\n", x[i], i)} }' <your_data_file_name> | sort -k2,3

# 4  
Old 01-04-2016
A Perl version to try:
Code:
perl -ne '@k=split /\s+/, $_, 2; $r{$k[1]} += $k[0]; END{for(sort keys %r){print "$r{$_}\t$_"}}' ernesto.input > ernesto.output

# 5  
Old 01-04-2016
You could also try:
Code:
awk '
last && last != $2 FS $3 {
	print	sum, last
}
last != $2 FS $3 {
	last = $2 FS $3
	sum = 0
}
{	sum += $1
}
END {	print sum, last
}' OFS="\t" file

and avoid the need for the sort.

As always, with any of these awk suggestions, if you're using a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep from file modified one minute ago

Hello, I have a list of files, an example below: -rw-r--r-- 1 smf_oper esg 910773 Jul 6 12:52 am1slc02_ACS_201607061242571_20346.cdr -rw-r--r-- 1 smf_oper esg 995838 Jul 6 12:52 am1slc01_ACS_201607061243125_19895.cdr -rw-r--r-- 1 smf_oper esg 557235 Jul 6 12:52... (5 Replies)
Discussion started by: nms
5 Replies

2. Shell Programming and Scripting

Check log file size every 10 minute. Alert if log not update

How to check log size every 10min. by script (can use crontab) if log size not change with alert "Log not update" Base run on SunOS 5.8 Generic_Virtual sun4u sparc SUNW,SPARC-Enterprise logFiles="log1.log log2.log" logLocation="/usr/home/test/log/" Out put. Tue Jan 31... (3 Replies)
Discussion started by: ooilinlove
3 Replies

3. Shell Programming and Scripting

Take minute per minute from a log awk

Hi, I've been trying to develop a script that performs the parsing of a log every 1 minute and then generating some statistics. I'm fairly new to programming and this is why I come to ask if I can lend a hand. this is my log: xxxx 16/04/2012 17:00:52 - xxxx714 - E234 - Time= 119 ms.... (8 Replies)
Discussion started by: jockx
8 Replies

4. Shell Programming and Scripting

Count of matched pattern occurences by minute and date in a log file

Anyone knows how to use AWK to achieve the following Sun Feb 12 00:41:01-00:41:59 Success:2 Fail:2 Sun Feb 12 00:42:01-00:42:59 Success:1 Fail:2 Sun Feb 12 01:20:01-01:20:59 Success:1 Fail:2 Mon Feb 13 22:41:01-22:41:59 Success:1 Fail:1 log file: Success Success Fail Fail ... (9 Replies)
Discussion started by: timmywong
9 Replies

5. Shell Programming and Scripting

Process file every minute

Hey guy's, How can I schedule this script using AWK, what I require is it runs every minute as new data will be added to the file? And I need to capture these data and save it to the output file. I tried using sleep 1 to allow the file to be processed but is not working. awk ' ... (35 Replies)
Discussion started by: James_Owen
35 Replies

6. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

7. UNIX for Dummies Questions & Answers

How to add an hour or a minute to a time?

Hi, The timestamp is June 06 2011 11:05AM i need 2 results. first, an hour added to it, June 06 2011 12:05AM second, a minute added to it, June 06 2011 11:06AM How can i do this? Also when it reaches 12:59, it needs to start from 1 again without giving the output as 13:00. it... (17 Replies)
Discussion started by: irudayaraj
17 Replies

8. UNIX for Dummies Questions & Answers

Perl add one minute to the given date

Hello Gurus, We have a query where we need to add one minute ( also we need to add 12 hrs in another query) to given date. we tried using some functionalities like 'perl -e @d= $run_dt + 600' . however we were not succeeded. Basically issue is we get date in following format "May18... (9 Replies)
Discussion started by: sandeeppvk
9 Replies

9. Shell Programming and Scripting

Determine if file changed within last minute

Is there a simple command to find or check if a file has been modified within the last minute? The mtime parameter from find cmd only allow days. I am trying to avoid timestamp parsing and do some sort of comparison as I'm real beginner at scripts. If this is the only way, any help is greatly... (3 Replies)
Discussion started by: ystee
3 Replies

10. Programming

linux 7.1 goes off every other minute

i have loaded linux 7.1 on my PC along with win98 the problem is that every other minute th system goes off please help me (1 Reply)
Discussion started by: vidya_harnal
1 Replies
Login or Register to Ask a Question