我正在尝试编写一个脚本,其中每个行元素将给出接下来N行(包括其自身)的平均值.我知道如何使用前面的行,如第N行将给出前面N行的平均值.这是脚本
awk '
BEGIN{
N = 5;
}
{
x = $2;
i = NR % N;
aveg += (x - X[i]) / N;
X[i] = x;
print $1, $2, aveg;
}' < file > aveg.txt
Run Code Online (Sandbox Code Playgroud)
文件看起来像这样
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
20 20
21 21
22 22
23 23
24 24
25 25
26 26
27 27
28 28
29 29
30 30
31 31
32 32
33 33
34 34
35 35
36 36
37 37
38 38
39 39
40 40
Run Code Online (Sandbox Code Playgroud)
我希望第一行具有接下来的5个元素的平均值,即
(1+2+3+4+5)/5=3
second row (2+3+4+5+6)/5=4
third row (3+4+5+6+7)/5=5
Run Code Online (Sandbox Code Playgroud)
等等.行应该看起来像
1 1 3
2 2 4
3 3 5
4 4 6 ...
Run Code Online (Sandbox Code Playgroud)
可以像上面显示的脚本一样简单地完成吗?我在考虑将行值指定为下面第n行的值,然后继续上面的脚本.但是,遗憾的是,我无法将行值分配给文件中的某个值.有人可以帮我写这个脚本并找到移动平均线.我也对shell中的其他命令持开放态度.
$ cat test.awk
BEGIN {
N=5 # the window size
}
{
n[NR]=$1 # store the value in an array
}
NR>=N { # for records where NR >= N
x=0 # reset the sum variable
delete n[NR-N] # delete the one out the window of N
for(i in n) # all array elements
x+=n[i] # ... must be summed
print n[NR-(N-1)],x/N # print the row from the beginning of window
} # and the related window average
Run Code Online (Sandbox Code Playgroud)
试试吧:
$ for i in {1..36}; do echo $i $i >> test.in ; done
$ awk -f test.awk test.in
1 3
2 4
3 5
...
30 32
31 33
32 34
Run Code Online (Sandbox Code Playgroud)
它可以在运行总和,添加当前和减去n[NR-N],如下所示:
BEGIN {
N=5
}
{
n[NR]=$1
x+=$1-n[NR-N]
}
NR>=N {
delete n[NR-N]
print n[NR-(N-1)],x/N
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
557 次 |
| 最近记录: |