Bash 使用awk查找列的平均值

Bash 使用awk查找列的平均值,bash,awk,Bash,Awk,我正在尝试使用类的awk查找第二列数据的平均值。这是我当前的代码,以及讲师提供的框架: #!/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

我正在尝试使用类的
awk
查找第二列数据的平均值。这是我当前的代码,以及讲师提供的框架:

#!/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
}
我得到一个错误,上面写着:

awk: avg.awk:11:        awk 'BEGIN{sum+=$2}' $name
awk: avg.awk:11:            ^ invalid char ''' in expression
我想我已经很接近了,但我真的不知道接下来该怎么办。代码不应该非常复杂,因为我们在课堂上看到的一切都是相当基本的。请让我知道

awk '{ sum += $2; n++ } END { if (n > 0) print sum / n; }'
sum
中的
$2
(第二列)中添加数字(变量由
awk
自动初始化为零),并增加行数(也可以通过内置变量NR处理)。最后,如果至少读取了一个值,则打印平均值

awk '{ sum += $2 } END { if (NR > 0) print sum / NR }'
如果您想使用shebang符号,您可以写:

#!/bin/awk

{ sum += $2 }
END { if (NR > 0) print sum / NR }
您还可以使用
printf()
和合适的格式(例如
%13.6e\n“
)控制平均值的格式

您还可以使用以下方法对代码进行一般化,以平均第N列(在本示例中为
N=2
):


我正在使用
tail-1
打印最后一行,该行应具有平均数

您的具体错误在第11行:

awk 'BEGIN{sum+=$2}'
这是一行,其中调用了
awk
,并指定了它的
BEGIN
块,但您已经在awk脚本中,因此不需要指定
awk
。您还希望在每行输入上运行
sum+=$2
,因此不希望它位于
BEGIN
块中。因此,这一行应该简单地改为:

sum+=$2
您也不需要以下行:

x=sum
read name
第一个只是创建了一个名为
x
sum
同义词,我不确定第二个是做什么的,但两者都不需要

这将使您的awk脚本:

#!/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
{
    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
}
Jonathan Leffler的回答为awk提供了一个表示相同固定代码的一行代码,并检查至少有1行输入(这将阻止任何除以零的错误)。如果

请尝试以下操作:

ls -l  | awk -F : '{sum+=$5} END {print "AVG=",sum/NR}'

NR是一个AWK内置变量,用于计算记录的数量

我对AWK不太了解,但这有帮助吗:这是一种非常奇怪的做事方式。它是有效的,但我想不出使用这种技术的好理由。这就成功了,非常感谢!我没有意识到,因为在awk脚本中,没有必要使用awk命令,新手犯了错误。再次感谢欢迎来到Stack Overflow。如果你为一个已经有几个月历史的问题添加了一个新的答案,并且包含了一个可接受的答案,那么你的新答案需要提供一些独特的新信息。目前还不清楚这是否起作用。不清楚为什么要将
ls-l
输入
awk
;也不清楚为什么要使用
作为字段分隔符。问题指出它需要对第2列求和,因此不清楚为什么要对第5列求和。如何同时打印文件名?
#!/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
{
    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
}
ls -l  | awk -F : '{sum+=$5} END {print "AVG=",sum/NR}'