从命令行获取一组数字的平均值的最快方法是什么?

发布于 2024-07-07 16:19:42 字数 89 浏览 16 评论 0原文

使用您期望在 nix 系统上找到的任何工具(事实上,如果您愿意,msdos 也很好),计算一组数字的平均值的最简单/最快的方法是什么,假设您有一个流或文件中的每行?

Using any tools which you would expect to find on a nix system (in fact, if you want, msdos is also fine too), what is the easiest/fastest way to calculate the mean of a set of numbers, assuming you have them one per line in a stream or file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

伴我老 2024-07-14 16:19:42
awk ' { n += $1 }; END { print n / NR }'

这会将总和累加到 n 中,然后除以项目数(NR = 记录数)。

适用于整数或实数。

awk ' { n += $1 }; END { print n / NR }'

This accumulates the sum in n, then divides by the number of items (NR = Number of Records).

Works for integers or reals.

別甾虛僞 2024-07-14 16:19:42

awk

awk '{total += $1; count++ } END {print total/count}'

Awk

awk '{total += $1; count++ } END {print total/count}'
梦途 2024-07-14 16:19:42

在 UNIX 上使用 Num-Utils

average 1 2 3 4 5 6 7 8 9

Using Num-Utils for UNIX:

average 1 2 3 4 5 6 7 8 9
平安喜乐 2024-07-14 16:19:42
perl -e 'while (<>) { $sum += $_; $count++ } print $sum / $count, "\n"';
perl -e 'while (<>) { $sum += $_; $count++ } print $sum / $count, "\n"';
心舞飞扬 2024-07-14 16:19:42

使用“st”(https://github.com/nferraz/st):

$ st numbers.txt
N      min   max    sum    mean  sd
10.00  1.00  10.00  55.00  5.50  3.03

指定一个选项以查看个人统计数据:(

$ st numbers.txt --mean
5.5

免责声明:我编写了这个工具:))

Using "st" (https://github.com/nferraz/st):

$ st numbers.txt
N      min   max    sum    mean  sd
10.00  1.00  10.00  55.00  5.50  3.03

Specify an option to see individual stats:

$ st numbers.txt --mean
5.5

(DISCLAIMER: I wrote this tool :))

心清如水 2024-07-14 16:19:42

在 Powershell 中,这将是

get-content .\meanNumbers.txt | measure-object -average

当然,这是详细的语法。 如果您使用别名输入,

gc .\meanNumbers.txt | measure-object -a

In Powershell, it would be

get-content .\meanNumbers.txt | measure-object -average

Of course, that's the verbose syntax. If you typed it using aliases,

gc .\meanNumbers.txt | measure-object -a
夜清冷一曲。 2024-07-14 16:19:42

珀尔。

@a = <STDIN>;

for($i = 0; $i < #@a; $i++)
{
   $sum += $a[i];
}

print $a[i]/#@a;

买者自负:我的语法可能有点古怪。

Perl.

@a = <STDIN>;

for($i = 0; $i < #@a; $i++)
{
   $sum += $a[i];
}

print $a[i]/#@a;

Caveat Emptor: My syntax may be a little whiffly.

不念旧人 2024-07-14 16:19:42

Ruby one liner

cat numbers.txt | ruby -ne 'BEGIN{$sum=0}; $sum=$sum+$_.to_f; END{puts $sum/$.}'

来源

Ruby one liner

cat numbers.txt | ruby -ne 'BEGIN{$sum=0}; $sum=$sum+$_.to_f; END{puts $sum/$.}'

source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文