Want to get average value for each hour


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Want to get average value for each hour
# 8  
Old 04-03-2015
It is because the ++ in count[subscript]++ is a post-increment operation. That expression returns the value it had before the value is incremented and then increments the variable to the new value. To have it return the new value (instead of the old value), you would use the pre-increment operation ++count[subscript].
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 04-03-2015
Got it! I need to dust off my old college C books.
# 10  
Old 04-14-2015
I have received hourly average for using below script, but i need daily average value.

Code:
awk -F'|' 'NR>2 {hour=" " substr($2,1,2); array[$1 hour] += $3 count[$1 hour]++ } END { for (a in array) {print a, array[a]/count[a] } } ' filec | sort 
03/02/2015 00 26.2439
03/02/2015 01 25.3431
03/02/2015 02 24.7481
03/02/2015 03 26.6714
03/02/2015 04 10.4339
03/02/2015 05 17.3723
03/02/2015 06 17.5681
03/02/2015 07 23.8506
03/02/2015 08 14.3764
03/02/2015 09 15.7681
03/02/2015 10 18.7348
03/02/2015 11 20.1648
03/02/2015 12 22.8531
03/02/2015 13 22.2181
03/02/2015 14 16.4239
03/02/2015 15 25.3914
03/02/2015 16 20.2731
03/02/2015 17 12.3039
03/02/2015 18 17.9764
03/02/2015 19 22.6573
03/02/2015 20 15.7089
03/02/2015 21 32.6081
03/02/2015 22 17.2506
03/02/2015 23 21.6848

# 11  
Old 04-14-2015
How about removing the hour?

Code:
 awk -F'|' 'NR>2 {array[$1] += $3 count[$1]++ } END { for (a in array) {print a, array[a]/count[a] } } '

This User Gave Thanks to mjf For This Post:
# 12  
Old 04-14-2015
Quote:
Originally Posted by Saravanan_0074
I have received hourly average for using below script, but i need daily average value.

Code:
awk -F'|' 'NR>2 {hour=" " substr($2,1,2); array[$1 hour] += $3 count[$1 hour]++ } END { for (a in array) {print a, array[a]/count[a] } } ' filec | sort 
03/02/2015 00 26.2439
03/02/2015 01 25.3431
03/02/2015 02 24.7481
03/02/2015 03 26.6714
03/02/2015 04 10.4339
03/02/2015 05 17.3723
03/02/2015 06 17.5681
03/02/2015 07 23.8506
03/02/2015 08 14.3764
03/02/2015 09 15.7681
03/02/2015 10 18.7348
03/02/2015 11 20.1648
03/02/2015 12 22.8531
03/02/2015 13 22.2181
03/02/2015 14 16.4239
03/02/2015 15 25.3914
03/02/2015 16 20.2731
03/02/2015 17 12.3039
03/02/2015 18 17.9764
03/02/2015 19 22.6573
03/02/2015 20 15.7089
03/02/2015 21 32.6081
03/02/2015 22 17.2506
03/02/2015 23 21.6848

As mjf already said, why gather each hour's data and calculate an average of each input line if you're trying to calculate a daily average.

But, in addition to that, there aren't any | characters in your input. So, with -F'|' in your code, there is nothing in $2 nor in $3 in your input. And, there is no header in this data, so, unless you want to skip data from the midnight hour and the 1am hour data in your daily averages, you don't want the NR>2. And, as has been said before, if you're going to use a single line scrunched up awk script, you MUST separate statements from each other with semicolons. And, since you data appears to be in month/day/year format instead of year/month/day, you need to modify your sort if the goal is to print the output in increasing date order when you run this code with data from December in one year and January in the next year...

Perhaps you wanted something more like:
Code:
awk '{array[$1]+=$3;count[$1]++}END{for(a in array){print a,array[a]/count[a]}}' filec|sort -k1.7,1.10 -k1.1,1.2 -k1.4,1.5

# 13  
Old 04-14-2015
Don,
I interpreted the data Saravanan_0074 included in his/her last post to be the output, not the input, of running the awk command (in which case you would need-F'|'and NR>2). The avg of 26.2439 (should be 26.24) for hour 00 appears to match the input in the original post although there appears to not be enough input data to confirm the hours beyond 00 in the output.

You are correct re: the sort of the date.
# 14  
Old 04-15-2015
Quote:
Originally Posted by mjf
Don,
I interpreted the data Saravanan_0074 included in his/her last post to be the output, not the input, of running the awk command (in which case you would need-F'|'and NR>2). The avg of 26.2439 (should be 26.24) for hour 00 appears to match the input in the original post although there appears to not be enough input data to confirm the hours beyond 00 in the output.

You are correct re: the sort of the date.
You may be correct. If that is the case, the only thing wrong with the Saravanan_0074's script was the missing semicolon (like the problem you had in post #2 in this script). But, it clearly is not going to provide a daily average; only hourly averages.

But, the stated goal in post #10 is to get the daily average, and the script in that post does NOT do that. And, it isn't clear which average is desired. Is the desire to get the arithmetic mean of the hourly arithmetic means? (That is what the script I suggested would provided if the data shown in post #10 was fed into it as input.) Is the desire to get the arithmetic mean of the individual data for each day? Or, is some other average desired???

Is there ever input for more than one day in the input file?
Are there always the same number of sample points for each day (and the same number of sample points each day for each hour)? What happens on days when there is a shift to or from daylight savings time?

Is the sort in the pipeline intended to sort average day values into date order? Or, is it intended to sort average hourly values for a single date into hour order? If daily averages and hourly averages are both supposed to be in the output, what is the sort order supposed to be? What output format is wanted for the daily average values?

Sometimes I get tired of trying to guess what requirements we are trying to meet.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Average of multiple time-stamped data every half hour

Hi All, Thank you for reading through my post and helping me figure out how I would be able to perform this task. For example: I have a list of continuous output collected into a file in the format as seen below: Date...........Time........C....A......... B ==========================... (5 Replies)
Discussion started by: terrychen
5 Replies

2. Shell Programming and Scripting

How to get next hour?

Hi guys I want any script to get me next hour For example Nexthour.sh 2013022823 It get me result 2013030100 Thanks a lot , I'm using Solaris 10 (5 Replies)
Discussion started by: teefa
5 Replies

3. Shell Programming and Scripting

How to convert 24 hour time to 12 hour timing?

Hi friends, I want to convert 24 hour timing to 12 hour please help me... my data file looks like this.. 13-Nov-2011 13:27:36 15.32044 72.68502 13-Nov-2011 12:08:31 15.31291 72.69807 16-Nov-2011 01:16:54 15.30844 72.74028 15-Nov-2011 20:09:25 15.35096 ... (13 Replies)
Discussion started by: nex_asp
13 Replies

4. UNIX for Dummies Questions & Answers

Load average spikes once an hour

Hi, I am getting a high load average, around 7, once an hour. It last for about 4 minutes and makes things fairly unusable for this time. How do I find out what is using this. Looking at top the only thing running at the time is md5sum. I have looked at the crontab and there is nothing... (10 Replies)
Discussion started by: sm9ai
10 Replies

5. Shell Programming and Scripting

Getting the last hour from a log.

I have a log like this: Jan 26 13:59:41 server2 ntpdate: step time server 91.189.94.4 offset 0.065456 sec Jan 26 13:59:41 server2 ntpd: ntpd 4.2.4p8@1.1612-o Fri Aug 6 22:49:54 UTC 2010 (1) Jan 26 13:59:41 server2 ntpd: precision = 1.000 usec Jan 26 13:59:41 server2 ntpd: ntp_io: estimated max... (2 Replies)
Discussion started by: Jotne
2 Replies

6. What is on Your Mind?

Power Hour?

I had some free time at work today so I decided to get a little practice with my shell scripts (I'm pretty new to the whole UNIX thing). I'm sure I'm not the only college student here so maybe this code will come in handy for future weekends. #!/bin/sh if then echo "No playlist... (0 Replies)
Discussion started by: thedoobieman5
0 Replies

7. Shell Programming and Scripting

Counting average data per hour

Hi i have log like this : Actually i will process the data become Anybody can help me ? (6 Replies)
Discussion started by: justbow
6 Replies

8. Shell Programming and Scripting

help in hour grep

i have this script that checks for yesterday date and also specific hour in that ----------------------------------------------------------------- TZ=`date +%Z`+24 ;a=`date +%Y-%m-%d %k` cd logs count=0 for i in DBMaint.log do cat $i | grep $a >> file12.txt done... (0 Replies)
Discussion started by: ali560045
0 Replies

9. UNIX for Dummies Questions & Answers

an hour less in 24 hour system

My program: __________________________________ #!/bin/ksh DAY=`date +%y%m%d` H=`date +%H` M=`date +%M` day=`date +%m/%d/%y` let h=$H-1 echo DAY $DAY echo H $H echo M $M echo day $day echo h $h _____________________________________ My result: (3 Replies)
Discussion started by: bobo
3 Replies

10. UNIX for Dummies Questions & Answers

1 Hour less

set DAY=`date +%y%m%d` set H=`date +%H` set M=`date +%M` mailx -s "$H-Mydata" myemail@mail.com<mydata I am looking to set the current hour to have 1 hour less in the subject header: For example: let's say the system time is 8 I want to have "7-Mydata" not "8-Mydata" Can some1... (6 Replies)
Discussion started by: bobo
6 Replies
Login or Register to Ask a Question