59

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.

4

4 回答 4

143
awk '{ sum += $2; n++ } END { if (n > 0) print sum / n; }'

在(变量通过 自动初始化为零)中添加$2(第二列)中的数字并增加行数(也可以通过内置变量 NR 处理)。最后,如果至少读取了一个值,则打印平均值。sumawk

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在此示例中):

awk -v N=2 '{ sum += $N } END { if (NR > 0) print sum / NR }'
于 2013-10-03T02:44:28.733 回答
18

您的具体错误与第 11 行有关:

awk 'BEGIN{sum+=$2}'

这是一个awk被调用的行,它的BEGIN块被指定——但你已经在一个 awk 脚本中,所以你不需要指定awk. 此外,您希望sum+=$2在每一行输入上运行,因此您不希望它在一个BEGIN块内。因此,该行应该简单地阅读:

sum+=$2

您也不需要以下行:

x=sum
read name

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

这将使您的 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 行输入(这会阻止任何除以零错误)。如果

于 2013-10-03T03:22:03.857 回答
5

尝试这个:

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

NR 是一个 AWK 内置变量,用于计算编号。记录

于 2014-02-10T06:51:06.747 回答
2
awk 's+=$2{print s/NR}' table | tail -1

tail -1用来打印应该具有平均数的最后一行...

于 2013-10-03T02:45:56.653 回答