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?
I couldn't just pass by... Here's my Haskell one-liner. It's actually quite readable:
Unfortunately there's no
ghci -e
to just run it, so it needs the main function, print and compilation.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.Here's a strange upgrade to make it work with floats:
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:
(same as brian d foy's answer, without 'END')
It is not easier to replace all new lines by
+
, add a0
and send it to theRuby
interpreter?If you do not have
irb
, you can send it tobc
, but you have to remove all newlines except the last one (ofecho
). It is better to usetr
for this, unless you have a PhD insed
.I prefer to use R for this:
Just to be ridiculous: