用awk找一列平均 [英] Use awk to find average of a column
问题描述
我试图找到数据的第二列使用 AWK
一类的平均水平。这是我目前的code,与我的导师提供的框架:
#!/ bin中/ AWK###此脚本当前打印处理的行的总数。
###你必须编辑这个脚本打印的第2列的平均
代替行数###。#code的此块是文件中的每一行执行
{
X =总和
读名
awk的'BEGIN {总和+ = $ 2}'
#脚本不应该为每一行打印出来的值
}
#结束块的最后一行被读取后处理
结束 {
#NR是一个变量等于行数的文件中
打印平均之和/ NR
#更改为打印而不是平均的行只是数
}
和我收到一个错误,指出:
AWK:avg.awk:11:awk的'BEGIN {总和+ = $ 2}'$名
AWK:avg.awk:11:恩$ P $ ^ pssion字符无效'''
我想我接近,但我真的不知道该去哪里从这里走。在code应该不会像我们所看到的在课堂上一直还算基本都极其复杂。请让我知道。
的awk'{总和+ = $ 2; ñ++} END {如果(N大于0)打印总和/ N; }
添加号码 $ 2
在总和(第二列)
(变量自动初始化为零通过 AWK
),并增加行(其数量也可以通过内置变量NR处理)。最后,如果有值读取的至少一个,输出平均
的awk'{总和+ = $ 2} END {如果(NR大于0)打印总和/ NR}'
如果您要使用的家当符号,你可以写:
#!/ bin中/ AWK{总和+ = $ 2}
END {如果(NR大于0)打印总和/ NR}
您也可以控制平均与的printf()
和一个合适的格式(%13.6e \\ N$格式C $ C>,例如)。
您也可以概括code均价第N列(与 N = 2
此示例中)使用:
的awk -v N = 2'{总和+ = $ N} END {如果(NR大于0)打印总和/ NR}'
I'm attempting to find the average of the second column of data using awk
for a class. This is my current code, with the framework my instructor provided:
#!/bin/awk
### This script currently prints the total number of rows processed.
### You must edit this script to print the average of the 2nd column
### instead of the number of rows.
# This block of code is executed for each line in the file
{
x=sum
read name
awk 'BEGIN{sum+=$2}'
# The script should NOT print out a value for each line
}
# The END block is processed after the last line is read
END {
# NR is a variable equal to the number of rows in the file
print "Average: " sum/ NR
# Change this to print the Average instead of just the number of rows
}
and I'm getting an error that says:
awk: avg.awk:11: awk 'BEGIN{sum+=$2}' $name
awk: avg.awk:11: ^ invalid char ''' in expression
I think I'm close but I really have no idea where to go from here. The code shouldn't be incredibly complex as everything we've seen in class has been fairly basic. Please let me know.
awk '{ sum += $2; n++ } END { if (n > 0) print sum / n; }'
Add the numbers in $2
(second column) in sum
(variables are auto-initialized to zero by awk
) and increment the number of rows (which could also be handled via built-in variable NR). At the end, if there was at least one value read, print the average.
awk '{ sum += $2 } END { if (NR > 0) print sum / NR }'
If you want to use the shebang notation, you could write:
#!/bin/awk
{ sum += $2 }
END { if (NR > 0) print sum / NR }
You can also control the format of the average with printf()
and a suitable format ("%13.6e\n"
, for example).
You can also generalize the code to average the Nth column (with N=2
in this sample) using:
awk -v N=2 '{ sum += $N } END { if (NR > 0) print sum / NR }'
这篇关于用awk找一列平均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!