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?
You can use awk:
Just for fun, lets do it with PDL, Perl's array math engine!
rcols
reads columns into a matrix (1D in this case) andsum
(surprise) sums all the element of the matrix.Here's another: