LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sum of output (https://www.linuxquestions.org/questions/linux-newbie-8/sum-of-output-776020/)

phsythax 12-16-2009 04:39 AM

sum of output
 
i've got a file, say;

Code:

6820
21046
148
85504
3526536
65152
4
4
318625092
689556
0
827656
4
5160
0
12
8530592
3183464

I would like to use addition to know the total sum of all these numbes. How would i do that?

AleLinuxBSD 12-16-2009 04:43 AM

You could use for example the language awk on a script bash.
Calculate sum and average of multiple lines - awk in bash

vinaytp 12-16-2009 04:57 AM

Quote:

Originally Posted by phsythax (Post 3793439)
i've got a file, say;

I would like to use addition to know the total sum of all these numbes. How would i do that?

Hello phsythax,

Can you use this script.sh ?

Code:

#!/bin/bash
sum=0
while read line
do
sum=`expr $line + $sum`
done < $1
echo "Sum is $sum"

execute it this way

Code:

sh script.sh /path/to/file/where/numbers/are/stored
Cheers !!!

evo2 12-16-2009 05:31 AM

Code:

awk 'BEGIN{s=0}{s+=$1}END{print s}' file
Cheers,

Evo2.

ghostdog74 12-16-2009 05:51 AM

try to put in some effort next time
Code:

awk '{s+=$0}END{print s}' file
echo $(tr "\n" "+" <file)0|bc


H_TeXMeX_H 12-16-2009 07:35 AM

There was a post in the programming section with this question:
http://www.linuxquestions.org/questi...6/#post3414768

pixellany 12-16-2009 08:28 AM

For completeness:

Open the file with a spreadsheet program and use the summation button.

ghostdog74 12-16-2009 08:38 AM

Quote:

Originally Posted by pixellany (Post 3793623)
For completeness:

Open the file with a spreadsheet program and use the summation button.

maybe he wants to automate?

evo2 12-16-2009 09:36 AM

Quote:

Originally Posted by pixellany (Post 3793623)
For completeness:

Open the file with a spreadsheet program and use the summation button.

:-) Nice one.

Cheers,

Evo2.

H_TeXMeX_H 12-16-2009 12:31 PM

Quote:

Originally Posted by pixellany (Post 3793623)
For completeness:

Open the file with a spreadsheet program and use the summation button.

or you can use xcalc, that's probably installed :)

catkin 12-16-2009 01:04 PM

Quote:

Originally Posted by ghostdog74 (Post 3793632)
maybe he wants to automate?

Then write a macro and open the file with a command that both opens it and runs the macro :). Truly there are many ways of skinning requirements/solutions cats.

ghostdog74 12-16-2009 07:07 PM

Quote:

Originally Posted by catkin (Post 3793993)
Truly there are many ways of skinning requirements/solutions cats.

yes, there are many ways, but some ways are simpler and not overkill. that's all i have to say.

evo2 12-16-2009 07:36 PM

Ohh, the comedy!

Evo2.


All times are GMT -5. The time now is 06:35 PM.