How can I quickly sum all numbers in a file?

2020-01-24 03:22发布

I have a file which contains several thousand numbers, each on it's own line:

34
42
11
6
2
99
...

I'm looking to write a script which will print the sum of all numbers in the file. I've got a solution, but it's not very efficient. (It takes several minutes to run.) I'm looking for a more efficient solution. Any suggestions?

27条回答
劳资没心,怎么记你
2楼-- · 2020-01-24 03:55

I couldn't just pass by... Here's my Haskell one-liner. It's actually quite readable:

sum <$> (read <$>) <$> lines <$> getContents

Unfortunately there's no ghci -e to just run it, so it needs the main function, print and compilation.

main = (sum <$> (read <$>) <$> lines <$> getContents) >>= print

To clarify, we read entire input (getContents), split it by lines, read as numbers and sum. <$> is fmap operator - we use it instead of usual function application because sure this all happens in IO. read needs an additional fmap, because it is also in the list.

$ ghc sum.hs
[1 of 1] Compiling Main             ( sum.hs, sum.o )
Linking sum ...
$ ./sum 
1
2
4
^D
7

Here's a strange upgrade to make it work with floats:

main = ((0.0 + ) <$> sum <$> (read <$>) <$> lines <$> getContents) >>= print
$ ./sum 
1.3
2.1
4.2
^D
7.6000000000000005
查看更多
仙女界的扛把子
3楼-- · 2020-01-24 03:56

You can do it with Alacon - command-line utility for Alasql database.

It works with Node.js, so you need to install Node.js and then Alasql package:

To calculate sum from TXT file you can use the following command:

> node alacon "SELECT VALUE SUM([0]) FROM TXT('mydata.txt')"
查看更多
Emotional °昔
4楼-- · 2020-01-24 03:57
cat nums | perl -ne '$sum += $_ } { print $sum'

(same as brian d foy's answer, without 'END')

查看更多
太酷不给撩
5楼-- · 2020-01-24 03:57

It is not easier to replace all new lines by +, add a 0 and send it to the Ruby interpreter?

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

If you do not have irb, you can send it to bc, but you have to remove all newlines except the last one (of echo). It is better to use tr for this, unless you have a PhD in sed .

(sed -e "s/$/+/" file|tr -d "\n"; echo 0)|bc
查看更多
爷的心禁止访问
6楼-- · 2020-01-24 03:58

I prefer to use R for this:

$ R -e 'sum(scan("filename"))'
查看更多
时光不老,我们不散
7楼-- · 2020-01-24 03:59

Just to be ridiculous:

cat f | tr "\n" "+" | perl -pne chop | R --vanilla --slave
查看更多
登录 后发表回答