我有100个数据文件,每个文件有1000行,它们看起来都是这样的:
0 0 0 0
1 0 1 0
2 0 1 -1
3 0 1 -2
4 1 1 -2
5 1 1 -3
6 1 0 -3
7 2 0 -3
8 2 0 -4
9 3 0 -4
10 4 0 -4
.
.
.
999 1 47 -21
1000 2 47 -21我开发了一个脚本,它应该取列2,3,4中每个值的平方,然后求和和平方根。就像这样:
temp = ($t1*$t1) + ($t2*$t2) + ($t3*$t3)
calc = $calc + sqrt ($temp)然后计算该值的平方,并在每个数据文件上对这些数字进行平均值,以输出每行的平均"calc“和每一行的平均"fluc”。
这些数字的意思是:第一个数字是步数,接下来的三个是x,y和z轴上的坐标。我试图找到“步骤”从原点开始的距离,这是用公式r = sqrt(x^2 + y^2 + z^2)计算的。接下来我需要r的涨落,它是用f = r^4或f = (r^2)^2来计算的。这些必须是100个数据文件的平均值,这使我想到:
r = r + sqrt(x^2 + y^2 + z^2)
avg = r/s同样,对于f,s是我使用sum=$(ls -l *.data | wc -l)计算出来的读取数据文件的数量。最后,我的最后一个计算是期望的r和平均r之间的偏差,这是使用最终值作为循环外的stddev = sqrt(fluc - (r^2)^2)计算的。
我创建的脚本是:
#!/bin/bash
sum=$(ls -l *.data | wc -l)
paste -d"\t" *.data | nawk -v s="$sum" '{
for(i=0;i<=s-1;i++)
{
t1 = 2+(i*4)
t2 = 3+(i*4)
t3 = 4+(i*4)
temp = ($t1*$t1) + ($t2*$t2) + ($t3*$t3)
calc = $calc + sqrt ($temp)
fluc = $fluc + ($calc*$calc)
}
stddev = sqrt(($calc^2) - ($fluc))
print $1" "calc/s" "fluc/s" "stddev
temp=0
calc=0
stddev=0
}'不幸的是,我在一定程度上收到了一个错误:
nawk: cmd. line:9: (FILENAME=- FNR=3) fatal: attempt to access field -1我对awk没有足够的经验,无法准确地找出我的错误所在,有人能为我指出正确的方向或给我一个更好的剧本吗?
预期的输出是一个文件,其中包括:
0 0 0 0
1 (calc for all 1's) (fluc for all 1's) (stddev for all 1's)
2 (calc for all 2's) (fluc for all 2's) (stddev for all 2's)
.
.
.发布于 2013-12-01 01:22:04
下面的脚本应该做您想做的事情。唯一可能不起作用的是分隔符的选择。在您的原始脚本中,您似乎有选项卡。我的解决方案假设空间。但改变这一点不应该是个问题。
它只是顺序地将所有文件排入nawk,而不首先计算文件。据我所知,这是不必要的。它没有试图跟踪文件中的位置,而是使用数组存储每个步骤的独立统计数据。最后,它遍历找到的所有步骤索引并输出它们。由于迭代没有排序,所以会有另一个管道进入Unix sort调用来处理这个问题。
#!/bin/bash
# pipe the data of all files into the nawk processor
cat *.data | nawk '
BEGIN {
FS=" " # set the delimiter for the columns
}
{
step = $1 # step is in column 1
temp = $2*$2 + $3*$3 + $4*$4
# use arrays indexed by step to store data
calc[step] = calc[step] + sqrt (temp)
fluc[step] = fluc[step] + calc[step]*calc[step]
count[step] = count[step] + 1 # count the number of samples seen for a step
}
END {
# iterate over all existing steps (this is not sorted!)
for (i in count) {
stddev = sqrt((calc[i] * calc[i]) + (fluc[i] * fluc[i]))
print i" "calc[i]/count[i]" "fluc[i]/count[i]" "stddev
}
}' | sort -n -k 1 # that' why we sort here: first column "-k 1" and numerically "-n"编辑
正如@edmorton所建议的那样,awk可以处理文件本身的加载。下面的增强版本移除对cat的调用,而是将文件模式作为参数传递给nawk。此外,正如@NictraSavios所建议的,新版本引入了对最后一步的统计数据输出的特殊处理。请注意,统计数据的收集仍然是为所有步骤完成的。在读取数据的过程中,要抑制这一点有点困难,因为那时我们还不知道最后一步是什么。虽然这可以通过一些额外的努力来完成,但是您可能会失去很多数据处理的健壮性,因为现在脚本并没有对以下方面做出任何假设:
增强脚本:
#!/bin/bash
nawk '
BEGIN {
FS=" " # set the delimiter for the columns (not really required for space which is the default)
maxstep = -1
}
{
step = $1 # step is in column 1
temp = $2*$2 + $3*$3 + $4*$4
# remember maximum step for selected output
if (step > maxstep)
maxstep = step
# use arrays indexed by step to store data
calc[step] = calc[step] + sqrt (temp)
fluc[step] = fluc[step] + calc[step]*calc[step]
count[step] = count[step] + 1 # count the number of samples seen for a step
}
END {
# iterate over all existing steps (this is not sorted!)
for (i in count) {
stddev = sqrt((calc[i] * calc[i]) + (fluc[i] * fluc[i]))
if (i == maxstep)
# handle the last step in a special way
print i" "calc[i]/count[i]" "fluc[i]/count[i]" "stddev
else
# this is the normal handling
print i" "calc[i]/count[i]
}
}' *.data | sort -n -k 1 # that' why we sort here: first column "-k 1" and numerically "-n"发布于 2013-12-01 06:47:57
您还可以使用:
awk -f c.awk *.datac.awk在哪里
{
j=FNR
temp=$2*$2+$3*$3+$4*$4
calc[j]=calc[j]+sqrt(temp)
fluc[j]=fluc[j]+calc[j]*calc[j]
}
END {
N=ARGIND
for (i=1; i<=FNR; i++) {
stdev=sqrt(fluc[i]-calc[i]*calc[i])
print i-1,calc[i]/N,fluc[i]/N,stdev
}
}https://stackoverflow.com/questions/20307007
复制相似问题