Middled Rolling average


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Middled Rolling average
# 1  
Old 07-24-2015
Middled Rolling average

Dear Help,

The input file is below ---

Code:
13 2238422.00000 101083.00000 0.89024 0.00416 0.00467 
14 2238318.00000 101090.00000 0.89100 0.00416 0.00467 
15 2238209.00000 101100.00000 0.90964 0.00424 0.00466 
16 2238104.00000 101104.00000 0.97568 0.00463 0.00475 
17 2237991.00000 101113.00000 0.89732 0.00419 0.00467 
18 2237874.00000 101111.00000 0.94532 0.00447 0.00473

I am using a script which does smoothing of the $4 values but I would like to print $2,$3,$5,$6 values intact. My script does not do that and changes all the other columns that need not require change.
Please suggest.

The script that I am using is -

Code:
BEGIN {
    m=int((n+1)/2)
}
{L[NR]=$4; sum+=$4}
NR>=m {d[++i]=$1}
NR>n {sum-=L[NR-n]}
NR>=n{
    a[++k]=sum/n
}
END {
    for (j=1; j<=k; j++)
        print d[j],$2,$3,a[j],$5,$6

Thanks

---------- Post updated at 10:22 AM ---------- Previous update was at 10:01 AM ----------

Dear Ravinder,

My present output is ---
Code:
12 2237766.00000 101107.00000 0.91629 0.00444 0.00481
13 2237766.00000 101107.00000 0.914292 0.00444 0.00481
14 2237766.00000 101107.00000 0.923956 0.00444 0.00481
15 2237766.00000 101107.00000 0.912776 0.00444 0.00481
16 2237766.00000 101107.00000 0.923792 0.00444 0.00481
17 2237766.00000 101107.00000 0.93041 0.00444 0.00481

The desired op is ---

Code:
12 2238537.00000 101085.00000 0.91629 0.00435 0.00457 
13 2238422.00000 101083.00000 0.91429 0.00416 0.00467 
14 2238318.00000 101090.00000 0.92395 0.00416 0.00467 
15 2238209.00000 101100.00000 0.91277 0.00424 0.00466 
16 2238104.00000 101104.00000 0.97237 0.00463 0.00475 
17 2237991.00000 101113.00000 0.93041 0.00419 0.00467

Thanks

---------- Post updated at 10:23 AM ---------- Previous update was at 10:22 AM ----------

And the input file is --
Code:
10 2238758.00000 101101.00000 0.91963 0.00429 0.00467 
11 2238636.00000 101092.00000 0.92736 0.00432 0.00466 
12 2238537.00000 101085.00000 0.95322 0.00435 0.00457 
13 2238422.00000 101083.00000 0.89024 0.00416 0.00467 
14 2238318.00000 101090.00000 0.89100 0.00416 0.00467 
15 2238209.00000 101100.00000 0.90964 0.00424 0.00466 
16 2238104.00000 101104.00000 0.97568 0.00463 0.00475 
17 2237991.00000 101113.00000 0.89732 0.00419 0.00467 
18 2237874.00000 101111.00000 0.94532 0.00447 0.00473 
19 2237766.00000 101107.00000 0.92409 0.00444 0.00481

---------- Post updated at 10:39 AM ---------- Previous update was at 10:23 AM ----------

Actually, I would like to change the values in 4th column($4) by what we call as rolling average. So, if you look at the
Code:
12 2238537.00000 101085.00000 0.95322 0.00435 0.00457

, here the value($4) i.e.
Code:
0.95322

will be replaced by (
Code:
(0.91963+0.9273+0.95322+0.89024+0.89100)/5

, which turns out to be
Code:
0.91629

. Hence we get the first line of the 'desired output ' file
Code:
12 2238537.00000 101085.00000 0.91629 0.00435 0.00457

. Similarly, it will follow for the next line. hope this clarifies everything.
Thanks a lot
# 2  
Old 07-24-2015
In the future, please show us your entire awk script. The script you showed us is at least missing a closing } and you have not shown us how the awk variable n was set.

If I correctly understand what you're trying to do, maybe this will do what you want:
Code:
awk -v n=5 '
BEGIN {	m = int((n + 1) / 2)
}
{	f[o = NR % n] = $1 OFS $2 OFS $3 OFS
	d[o] = $4
	l[o] = OFS $5 OFS $6
}
NR >= n {
	for(c = i = 0; i < n; i++)
		c += d[i]
	c /= n
	i = (NR - m + 1) % n
	printf("%s%.5f%s\n", f[i], c, l[i])
}' file

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.

With the sample input you provided, the above script produces the output:
Code:
12 2238537.00000 101085.00000 0.91629 0.00435 0.00457
13 2238422.00000 101083.00000 0.91429 0.00416 0.00467
14 2238318.00000 101090.00000 0.92396 0.00416 0.00467
15 2238209.00000 101100.00000 0.91278 0.00424 0.00466
16 2238104.00000 101104.00000 0.92379 0.00463 0.00475
17 2237991.00000 101113.00000 0.93041 0.00419 0.00467

I assume that the differences between the results shown above and the results you said you wanted are rounding differences.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 11-13-2015
Hi Help,

Is it possible to use the above code (Cragun's) in a situation like below ---

The I/P file is --
Code:
10 2238758.00000 101101.00000 0.91963 0.00429 0.00467 
11 2238636.00000 101092.00000 0.92736 0.00432 0.00466 
12 2238537.00000 101085.00000 0.95322 0.00435 0.00457 
13 2238422.00000 101083.00000 0.00000 0.00416 0.00467 
14 2238318.00000 101090.00000 0.89100 0.00416 0.00467 
15 2238209.00000 101100.00000 0.90964 0.00424 0.00466 
16 2238104.00000 101104.00000 0.97568 0.00463 0.00475 
17 2237991.00000 101113.00000 0.00000 0.00419 0.00467 
18 2237874.00000 101111.00000 0.94532 0.00447 0.00473 
19 2237766.00000 101107.00000 0.92409 0.00444 0.00481

I am thinking of a O/P which will only have $4 replaced by rolling average only when $4==0 ( In this case when $1=13 and 17)

Thanks for if any suggestion or way-around.

Last edited by Indra2011; 11-13-2015 at 09:10 PM..
# 4  
Old 11-14-2015
And I guess you don't want the zero value to take part in the average computation? What exactly do you want to be filled in?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-14-2015
Yes, RudiC, I don't want the zero to be part of the computation.

Thanks
# 6  
Old 11-14-2015
And the second question?
This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-14-2015
Code:
$4 for NR=13

is equal to
Code:
($4 of NR=11 + $4 of NR=12 + $4 of NR = 14 +$4 of NR = 15)/4

.

So excluded 0 from computation. If we have two zeroes then we divide the summation by 3 and likewise.

Last edited by Don Cragun; 11-14-2015 at 06:36 PM.. Reason: Fix CODE and ICODE tags.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get average and percentage non zero value

How to calculate percentage non zero value occurrence base on value col 1 and 2 2017 a 0 2017 a 2 2017 a 4 2017 a 2 2017 a 0 2017 b 2 2017 b 6 2016 a 2 2016 a 2 2016 b 2 2016 b 8 2016 b 0 2016 b 0 2016 c 2 2016 c 2 2016 c 0 i manage to get average # awk '{A++;B+=$3}END{for(X in... (1 Reply)
Discussion started by: before4
1 Replies

2. UNIX for Advanced & Expert Users

Want to get average value for each hour

I want to get CPU average value only (not required user CPU & memory) with each hours on individual date. The sample output is below | | | User |Memory| User | Date | Time |CPU %|CPU % | % |Mem % | 03/02/2015|00:00:00| 24.56| 20.66| 89.75| 63.48|... (13 Replies)
Discussion started by: Saravanan_0074
13 Replies

3. UNIX for Dummies Questions & Answers

Rolling back SQL transaction

Can some one help me related to .sql file issue. I have a .sqlfile and tried to read the file thru unix. In the .sqlfile I have error rows as well and when error comes I dont want to proceed further and need to roll back all the transactions. sample .sql file below insert into test... (2 Replies)
Discussion started by: sri_aue
2 Replies

4. Linux

Rolling Back an Update

I am writing a software product and hope that it will work on a variety of Linux distributions. At the moment, I am trying to create some kind of Linux version of patches/upgrades of installed software. Gathering information on available updates isn't hard, nor is installation of updates, but I... (27 Replies)
Discussion started by: Brandon9000
27 Replies

5. AIX

rolling back Technology Level

Hi, is it possible to roll back currently updated Technology level ? what are steps required? Regards, Manoj (2 Replies)
Discussion started by: manoj.solaris
2 Replies

6. Shell Programming and Scripting

log rolling

Hi, I'm thinking of running a script via cron (every hour) to do the log rolling. The file is "file.txt" and there going to be 10 files rolling (file.txt.n). The file is being written constantly by an application. The script will do the following: 1. cat file.txt > file.txt.0 2. cat... (0 Replies)
Discussion started by: chaandana
0 Replies

7. Shell Programming and Scripting

count average

Hi Friends, Can any one help me with count average of student marks in this file (i can not change structure of the input file): input file: 1 - student ID 2 - student name 3 - group ID 4 - teacher ID 5 - marks (numbers of marks are different) 1:John Smith:2:3:2 3 4 5 2:Mark... (1 Reply)
Discussion started by: mleplawy
1 Replies

8. UNIX for Dummies Questions & Answers

average value

If I have a file like this, could anyone please guide me how to find the average value in each metrix. The file has got about 130,000 metrixs. Grid-ref= 142, 235 178 182 203 240 273 295 289 293 283 262 201 176 167 187 187 246 260 282 299 312 293 276 230 191 169 ... (2 Replies)
Discussion started by: su_in99
2 Replies

9. UNIX for Dummies Questions & Answers

Rolling back time

Hi all, Have a small problem. Back in October the pervious sys-admin (of a client's company) made the necessary adjustments to the system clock for daylight savings (Sydney time - +11 GMT). As far as I can gather, they just amended the time - NO TIMEZONE !?! Is there an effective and safe... (5 Replies)
Discussion started by: Cameron
5 Replies
Login or Register to Ask a Question