我有一个包含数千个数字的文件,每个数字都在自己的行上:

34
42
11
6
2
99
...

我想写一个脚本,它将打印文件中所有数字的总和。我有一个解决办法,但不是很有效。(运行需要几分钟。)我在寻找一个更有效的解决方案。有什么建议吗?


当前回答

Bash变体

raw=$(cat file)
echo $(( ${raw//$'\n'/+} ))

$ wc -l file
10000 file

$ time ./test
323390

real    0m3,096s
user    0m3,095s
sys     0m0,000s

这里发生了什么?读取一个文件的内容到$raw var中,然后通过将所有新行更改为“+”来从该var创建数学语句

其他回答

tcl中的一个:

#!/usr/bin/env tclsh
set sum 0
while {[gets stdin num] >= 0} { incr sum $num }
puts $sum

更简洁:

# Ruby
ruby -e 'puts open("random_numbers").map(&:to_i).reduce(:+)'

# Python
python -c 'print(sum(int(l) for l in open("random_numbers")))'

C总是以速度取胜:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    ssize_t read;
    char *line = NULL;
    size_t len = 0;
    double sum = 0.0;

    while (read = getline(&line, &len, stdin) != -1) {
        sum += atof(line);
    }

    printf("%f", sum);
    return 0;
}

1M数字的计时(与我的python答案相同的机器/输入):

$ gcc sum.c -o sum && time ./sum < numbers 
5003371677.000000
real    0m0.188s
user    0m0.180s
sys     0m0.000s

用+替换所有的新行,加一个0并把它发送给Ruby解释器不是更容易吗?

(sed -e "s/$/+/" file; echo 0)|irb

如果你没有irb,你可以把它发送到bc,但是你必须删除所有的换行符,除了最后一个(echo)。最好使用tr,除非您拥有sed的博士学位。

(sed -e "s/$/+/" file|tr -d "\n"; echo 0)|bc

在shell中使用awk,我使用下面的脚本来这样做:

    #!/bin/bash


total=0;

for i in $( awk '{ print $1; }' <myfile> )
do
 total=$(echo $total+$i | bc )
 ((count++))
done
echo "scale=2; $total " | bc