Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-16-2009, 04:39 AM
|
#1
|
Member
Registered: Oct 2005
Location: Denmark
Distribution: Gentoo & XP pro for gaming
Posts: 152
Rep:
|
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?
|
|
|
12-16-2009, 04:57 AM
|
#3
|
Member
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707
Rep:
|
Quote:
Originally Posted by phsythax
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 !!!
|
|
|
12-16-2009, 05:31 AM
|
#4
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Code:
awk 'BEGIN{s=0}{s+=$1}END{print s}' file
Cheers,
Evo2.
Last edited by evo2; 12-16-2009 at 05:33 AM.
|
|
|
12-16-2009, 05:51 AM
|
#5
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
try to put in some effort next time
Code:
awk '{s+=$0}END{print s}' file
echo $(tr "\n" "+" <file)0|bc
|
|
|
12-16-2009, 08:28 AM
|
#7
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
For completeness:
Open the file with a spreadsheet program and use the summation button.
|
|
|
12-16-2009, 08:38 AM
|
#8
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by pixellany
For completeness:
Open the file with a spreadsheet program and use the summation button.
|
maybe he wants to automate?
|
|
|
12-16-2009, 09:36 AM
|
#9
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Quote:
Originally Posted by pixellany
For completeness:
Open the file with a spreadsheet program and use the summation button.
|
:-) Nice one.
Cheers,
Evo2.
|
|
|
12-16-2009, 12:31 PM
|
#10
|
LQ Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
|
Quote:
Originally Posted by pixellany
For completeness:
Open the file with a spreadsheet program and use the summation button.
|
or you can use xcalc, that's probably installed 
|
|
|
12-16-2009, 01:04 PM
|
#11
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by ghostdog74
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.
|
|
|
12-16-2009, 07:07 PM
|
#12
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by catkin
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.
|
|
|
12-16-2009, 07:36 PM
|
#13
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Ohh, the comedy!
Evo2.
|
|
|
All times are GMT -5. The time now is 09:35 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|