Average for every day


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Average for every day
# 1  
Old 11-01-2012
Average for every day

Hi

i have data like - Fieldseperator is "\t" -..

Code:
...
05/18/12-23:40  12.0647 96.4762 140.746 19.4222 38.0837 17.1549
05/18/12-23:50  11.9463 97.7457 139.447 20.4776 29.8511 17.0144
05/19/12-00:00  11.6922 94.7384 130.364 18.5693 28.28   15.6425
05/19/12-00:10  10.8512 87.547  113.844 15.6814 19.8352 13.6885
05/19/12-00:20  10.91   88.8967 112.021 14.0773 16.0285 13.2715
05/19/12-00:30  10.5567 85.8925 105.703 13.2694 26.9759 13.171
....

Data over several days in the same file. I want to calculate by awk the average of $5 and $7 per day.
Unfortunatelly i have no idea how.
Can someone please help me?
Thanks in advance!
IMPe
# 2  
Old 11-01-2012
try

Code:
awk '{ split($1,P,"-");a[P[1]]=a[P[1]]?a[P[1]]+$5:$5;b[P[1]]=b[P[1]]?b[P[1]]+$7:$7;B[P[1]]++}END{for(i in a){print i,(a[i]/B[i]),(b[i]/B[i])}}' file

This User Gave Thanks to pamu For This Post:
# 3  
Old 11-01-2012
Quote:
Originally Posted by pamu
try

Code:
awk '{ split($1,P,"-");a[P[1]]=a[P[1]]?a[P[1]]+$5:$5;b[P[1]]=b[P[1]]?b[P[1]]+$7:$7;B[P[1]]++}END{for(i in a){print i,(a[i]/B[i]),(b[i]/B[i])}}' file

Thanks a lot for your fast answer. It seams that the sympatic "oneliner" calculate the average correct - really great - but it is mixing up the chronology, like ....

Code:
...
07/28/12 23.3401 23.397
09/16/12 24.858 14.2039
08/08/12 17.6552 8.55343
06/28/12 6.42296 6.87038
09/24/12 12.6617 8.59278
08/16/12 39.6912 21.3547
07/08/12 2.37503 1.92747
05/28/12 7.58515 5.95629
08/24/12 22.6712 14.3557
07/16/12 29.1969 12.5476
06/08/12 10.748 7.59749
09/04/12 28.6064 18.0908
07/24/12 19.5603 8.51454
...

Did you have any idea, what the script is doing. i thought, that it will be calkculatet line by line.

Thanks in advance!
IMPe
# 4  
Old 11-01-2012
You can just add sort to it..

Code:
awk '{ split($1,P,"-");a[P[1]]=a[P[1]]?a[P[1]]+$5:$5;b[P[1]]=b[P[1]]?b[P[1]]+$7:$7;B[P[1]]++}END{for(i in a){print i,(a[i]/B[i]),(b[i]/B[i])}}' file | sort

Or do STH like this..

Code:
awk '
function prnt()
{
    print s,(a[s]/B[s]),(b[s]/B[s])
}        
{split($1,P,"-");if(!B[P[1]]++ && s){prnt()}; a[P[1]]=a[P[1]]?a[P[1]]+$5:$5 ;b[P[1]]=b[P[1]]?b[P[1]]+$7:$7;s=P[1]}
END{prnt()}' file

This User Gave Thanks to pamu For This Post:
# 5  
Old 11-01-2012
exactly what i want, thanks a lot!
# 6  
Old 11-01-2012
Alternatively:
Code:
awk -F'[\t-]' 'p!=$1{if(p)print p, a/n, b/n; a=b=n=0; p=$1}{a+=$6; b+=$8; n++} END{print p, a/n, b/n}' file

# 7  
Old 11-01-2012
... or a blend of the two solutions:

Quote:
awk '{d5[$1]+=$6;d7[$1]+=$8;n[$1]+=1}END{for (d in d5) print d,d5[d]/n[d],d7[d]/n[d]}' FS="[- ]" file
@Scrutinizer your snippet doesn't seem to return the expected result. At least on my sample file. Or is it me?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Julian day to dates in YEAR-MONTH-DAY

hello, I have many files called day001, day002, day003 and I want to rename them by day20070101, day20070102, etc. I need to do it for several years and leap years as well. What is the best way to do it ? Thank you. (1 Reply)
Discussion started by: Ggg
1 Replies

2. UNIX for Dummies Questions & Answers

Move the files between Current day & a previous day

Hi All, I have a requirement where I need to first capture the current day & move all the files from a particular directory based on a previous day. i.e move all the files from one directory to another based on current day & a previous day. Here is what I am trying, but it gives me errors.... (2 Replies)
Discussion started by: dsfreddie
2 Replies

3. Shell Programming and Scripting

Script to check if last modified day is previous day

Hi, I would like to write a script that checks if a file ('counter') was modified the previous day, if so erase its contents and write 00000000 into it. For e.g. if the file 'counter' was last modified at 11.30pm on 24th May and the script runs at 12.15am of 25th May, it should erase it's... (1 Reply)
Discussion started by: hegdepras
1 Replies

4. Shell Programming and Scripting

Code creates day 32 instead of 1st day of next month.

I am using the code below modified from a post I saw here regarding having the script write out future dates. The problem is that instead of making 8/1 it makes 7/32! Please help! yy=`date +%Y` mm=`date +%m` dd=`date +%d` echo "Today is : $yy $mm $dd" #!/usr/bin/ksh date '+%m... (5 Replies)
Discussion started by: libertyforall
5 Replies

5. Solaris

May i know the day to day activities of a Solaris system administrator?

Recently i have attended a telephonic interview. As i dont have work experience in solaris i was not able to deliver correct answer for this question. Your answer will help for the people like me who is looking to become Solaris System administrator. (4 Replies)
Discussion started by: Sesha
4 Replies

6. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

7. Solaris

/var size is increasing day by day

Hi experts, I am facing a big problem. i use solaris 9. i found size of /var is increasing day by. snapshot of a yester moring- /dev/vx/dsk/var 15G 14G 1.1G 94% /var snapshot of a yesterday everning- /dev/vx/dsk/var 15G 14G 824M 95% /var I am... (11 Replies)
Discussion started by: thepurple
11 Replies

8. Shell Programming and Scripting

Write a shell script to find whether the first day of the month is a working day

Hi , I am relatively new to unix... Can u pls help me out to find out if the first day of the month is a working day ie from (Monday to Friday)...using Date and If clause in Korn shell.. This is very urgent. Thanks for ur help... (7 Replies)
Discussion started by: phani
7 Replies

9. Shell Programming and Scripting

How to compare prev day file to current day file

Hi all: I am new to this board and UNIX programming so please forgive if I don't explain something correctly. I am trying to write a script to keep track of our links, we link one program written for Client A to Client B's directory. What we want to do is to keep track of our linked programs... (1 Reply)
Discussion started by: Smurtzy
1 Replies
Login or Register to Ask a Question