linux - 创建一个 awk 脚本来读取另一个 awk 文件的输出并实现总和和平均值

标签 linux shell awk

我有一个名为 random.awk 的 awk 脚本,它生成近似于正态分布的随机数

#!/usr/bin/awk -f
#the sum of three uniform [-1,1] random variables is used to get the approximate normal distribution
#the first argument the number of random numbers to be generated, the default is 100
#the second argument is to get the mean value, the default is 0
#the third argument is to get the standard deviation, the default is 1

function prand(){ 
    return(((rand()*2)-1)+((rand()*2)-1)+((rand()*2)-1));
}

BEGIN{
    # establish default values
    countnumber=(length(ARGV[1])==0?100:ARGV[1]);
    meanvalue=(length(ARGV[2])==0?0.0:ARGV[2]);
    stdeviation=(length(ARGV[3])==0?1.0:ARGV[3]);

    for(i=0;i<count;i++){
    printf("%.16e\n",prand()*stdeviation+meanvalue)
    }
}

我如何创建一个 awk 文件以使用 random.awk 的输出作为输入并实现朴素求和和均值计算?

最佳答案

像您刚才那样创建一个 awk 脚本,用于生成随机值,但执行均值和求和计算,并使用 shell 管道符号 | 将第一个脚本的输出通过管道传输到第二个脚本。如果您需要更多帮助,则需要在问题中提供更多信息。

关于linux - 创建一个 awk 脚本来读取另一个 awk 文件的输出并实现总和和平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23658491/

相关文章:

c++ - 为什么 glGenVertexArrays 是为 C 程序定义的,而不是 Linux 上的 C++ 程序?

parsing - ctags:获取C函数结束行号

bash - 如何删除匹配模式之前和之后的行

bash - 按字母顺序按文件名顺序列出文件

linux - 为多个文件分配一个文件头,Linux

ubuntu 中的 C++(linux 平台)

c - setuid 不适用于非 root 用户

linux - 直接打开到 Xserver 的套接字

javascript - node.js shell 命令执行

bash - 我如何为子进程申请 'set -e'?