Floatingaverage.awk: Difference between revisions

From Federal Burro of Information
Jump to navigationJump to search
(New page: suppose your data looks like this: <pre> 1210258820 13 1210258825 17.4 1210258830 10 1210258835 14 1210258840 8 1210258845 12.4 1210258850 9 1210258855 13.2 1210258860 8.6 1210258865 4.8...)
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 104: Line 104:
}
}
</pre>
</pre>
[[Category:Script]]

Latest revision as of 14:17, 10 July 2020

suppose your data looks like this:

1210258820 13
1210258825 17.4
1210258830 10
1210258835 14
1210258840 8
1210258845 12.4
1210258850 9
1210258855 13.2
1210258860 8.6
1210258865 4.8
1210258870 0.8
1210258875 0.6
1210258880 5.2
1210258885 1.4
1210258890 2.8
1210258895 0.8
1210258900 1.4
1210258905 19.8
1210258910 13
1210258915 9.6
1210258920 5.6
1210258925 0.8
1210258930 5.4
1210258935 0.8
1210258940 7
1210258945 0.2
1210258950 1.2
1210258955 6.2
1210258960 10
1210258965 7.8
1210258970 8.8
1210258975 17.2
1210258980 3
1210258985 0.8

And you want the 10 point floating average of the second coloumn ($2). You can use this script, like this:

cat datafile | awk -f floatingaverage.awk > datafile_with_floating_average

the results:

1210258820 13 1.3
1210258825 17.4 3.04
1210258830 10 4.04
1210258835 14 5.44
1210258840 8 6.24
1210258845 12.4 7.48
1210258850 9 8.38
1210258855 13.2 9.7
1210258860 8.6 10.56
1210258865 4.8 11.04
1210258870 0.8 11.12
1210258875 0.6 9.88
1210258880 5.2 8.66
1210258885 1.4 7.8
1210258890 2.8 6.68
1210258895 0.8 5.96
1210258900 1.4 4.86
1210258905 19.8 5.94
1210258910 13 5.92
1210258915 9.6 6.02
1210258920 5.6 6.1
1210258925 0.8 6.1
1210258930 5.4 6.58
1210258935 0.8 6.14
1210258940 7 6.7
1210258945 0.2 6.44
1210258950 1.2 6.48
1210258955 6.2 6.96
1210258960 10 5.98
1210258965 7.8 5.46
1210258970 8.8 5.38
1210258975 17.2 6.54
1210258980 3 6.76
1210258985 0.8 6.3

The first 9 data points are invalid as there isn't enough data at that point.

Script:

# calculates the floating average of $2 and puts it in $3
BEGIN{
        fasize = 10
}
{
        #print "working on ", $0
        for (x = 0; x <= fasize; x++) {
                #print x
                myarr[x] = myarr[x+1]
                #print "myarr" , myarr[x] , myarr[x+1]
        }
        myarr[fasize] = $2
        sum=0
        for (j = 0; j <= fasize; j++)
                sum=sum+myarr[j]
        myaverage = sum / fasize
        print $1, $2, myaverage
}