Sponsored Content
Full Discussion: Moving Averages SMA !
Top Forums Shell Programming and Scripting Moving Averages SMA ! Post 302527174 by csierra on Thursday 2nd of June 2011 05:35:55 PM
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

 

6 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 08:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy