【问题标题】:Use awk to find average of a column [duplicate]使用awk查找列的平均值[重复]
【发布时间】:2013-10-09 14:13:56
【问题描述】:

我正在尝试使用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

我想我已经很接近了,但我真的不知道从这里去哪里。代码不应该非常复杂,因为我们在课堂上看到的一切都是相当基本的。请告诉我。

【问题讨论】:

标签: bash awk


【解决方案1】:
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):

awk -v N=2 '{ sum += $N } END { if (NR > 0) print sum / NR }'

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 成功了,非常感谢!我没有意识到,因为在 awk 脚本中不需要 awk 命令,菜鸟错误。再次感谢
    【解决方案3】:

    试试这个:

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

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

    【讨论】:

    • 欢迎来到 Stack Overflow。如果您为几个月前的问题添加了一个新答案,其中包括一个已接受的答案,那么您的新答案需要提供一些独特的新信息。目前尚不清楚这是否有效。您为什么将ls -l 输入awk 并不明显;也不清楚您为什么使用 : 作为字段分隔符。问题表明它需要对第 2 列求和,因此为什么对第 5 列求和并不明显。
    • 如何同时打印文件名?
    【解决方案4】:
    awk 's+=$2{print s/NR}' table | tail -1
    

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

    【讨论】:

    • 一种非常奇怪的做事方式。它有效,但我想不出使用这种技术的充分理由。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 2018-07-12
    • 2012-02-20
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多