Moving Averages SMA !


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving Averages SMA !
# 1  
Old 06-02-2011
Question Moving Averages SMA !

Hello,

I am trying to create a awk script to calculate the simple moving average of two fields but I got only the result of the first field.

Could you please help me to fix this awk shell script

Code:
awk -F, -v points=5 '

 { 
 a[NR % points] = $2;
 b[NR % points] = $3;
 if(NR>=points) {
 total_a = 0;
 total_b = 0;
 for (i = 0; i < points; ++i) 
     { total_a = total_a + a[i]; }
     { total_b = total_b + b[i]; }
 print ($1, $2, total_a/points, $3, total_b/points);
 }
 }' data.test


"data.test"
001,10,100
002,11,110
003,12,111
004,13,111
005,14,110
006,14,110
007,15,109
008,14,108
009,13,107
010,13,108

output expected

Row     Field2  MA      Field3  MA
5    14    12    110    108.4
6    14    12.8    110    110.4
7    15    13.6    109    110.2
8    14    14    108    109.6
9    13    14    107    108.8
10    13    13.8    108    108.4

# 2  
Old 06-06-2011
Code:
awk -F, -v points=5 '

 { 
 a[NR % points] = $2;
 b[NR % points] = $3;
 if(NR>=points) {
 total_a = 0;
 total_b = 0;
 for (i = 0; i < points; ++i) 
     { total_a = total_a + a[i]; 
      total_b = total_b + b[i]; }
 print ($1, $2, total_a/points, $3, total_b/points);
 }
 }' data.test

This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SMA (Single Moving Average) and Standard Deviation

Hello Team, I am using the following awk script to calculate the SMA (Single Moving Average) for an specific period but now I would like to include the standard deviation output. Could you please help me to modify this awk shell script awk -F, -v points=5 ' { a = $2; ... (4 Replies)
Discussion started by: csierra
4 Replies

2. Solaris

Cannot get sma (net-snmp) service to start.

Hey everyone, so I recently installed sma and disabled snmpdx for system monitoring in Solarwinds on 7 Oracle servers running Solaris 10. It was running just fine until we had to shutdown all systems and power back up (hurricane). Since then I cannot get the sma service to start, it goes into... (4 Replies)
Discussion started by: Zeus18
4 Replies

3. How to Post in the The UNIX and Linux Forums

Daily averages...

I have date file like below.. 1995 1 2 10 29 38.6706 -6.53823 41.9201 1995 1 2 10 29 -49.2477 -4.59733 17.2704 1995 1 2 10 29 -49.2369 -4.48045 8.61348 1995 1 3 8 48 -42.2643 ... (3 Replies)
Discussion started by: athithi
3 Replies

4. Solaris

SAR averages question

Hi all Bit of a silly question, but if I run sar to get the CPU stats (something like this): sar -u 300 1 The figures that are returned, is it in the above case the average over 300 seconds, or does it just wait for 300 seconds before obtaining the readings? (1 Reply)
Discussion started by: kinetik
1 Replies

5. Shell Programming and Scripting

Calculate Averages !

Hi, I have a file with more than 2,000 rows like this: 05/26/2011,1200,1500 I would like to create a awk shell script that calculate the price average of the second and third field each 5,10 and 20 rows or ask me for the values, starting at first row. Finally compare the average value... (1 Reply)
Discussion started by: csierra
1 Replies

6. Programming

Python Script to calculate averages

I'm fairly new to python so bare with me. I'm trying to write a script that would calculate my class average from exams. The script will look at a text file formatted like this: Calculus 95 90 92 Physics 80 85 92 History 90 82 83 95 The output should look like this: Calculus 92.33... (2 Replies)
Discussion started by: jl487
2 Replies
Login or Register to Ask a Question