LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 12-16-2009, 03:39 AM   #1
phsythax
Member
 
Registered: Oct 2005
Location: Denmark
Distribution: Gentoo & XP pro for gaming
Posts: 152

Rep: Reputation: 30
Question 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?
 
Old 12-16-2009, 03:43 AM   #2
AleLinuxBSD
Member
 
Registered: May 2006
Location: Italy
Distribution: Ubuntu, ArchLinux, Debian, SL, OpenBSD
Posts: 274

Rep: Reputation: 42
You could use for example the language awk on a script bash.
Calculate sum and average of multiple lines - awk in bash
 
Old 12-16-2009, 03:57 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
Quote:
Originally Posted by phsythax View Post
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 !!!
 
Old 12-16-2009, 04:31 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Code:
awk 'BEGIN{s=0}{s+=$1}END{print s}' file
Cheers,

Evo2.

Last edited by evo2; 12-16-2009 at 04:33 AM.
 
Old 12-16-2009, 04:51 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
try to put in some effort next time
Code:
awk '{s+=$0}END{print s}' file
echo $(tr "\n" "+" <file)0|bc
 
Old 12-16-2009, 06:35 AM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
There was a post in the programming section with this question:
http://www.linuxquestions.org/questi...6/#post3414768
 
Old 12-16-2009, 07:28 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
For completeness:

Open the file with a spreadsheet program and use the summation button.
 
Old 12-16-2009, 07:38 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany View Post
For completeness:

Open the file with a spreadsheet program and use the summation button.
maybe he wants to automate?
 
Old 12-16-2009, 08:36 AM   #9
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by pixellany View Post
For completeness:

Open the file with a spreadsheet program and use the summation button.
:-) Nice one.

Cheers,

Evo2.
 
Old 12-16-2009, 11:31 AM   #10
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by pixellany View Post
For completeness:

Open the file with a spreadsheet program and use the summation button.
or you can use xcalc, that's probably installed
 
Old 12-16-2009, 12:04 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ghostdog74 View Post
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.
 
Old 12-16-2009, 06:07 PM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by catkin View Post
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.
 
Old 12-16-2009, 06:36 PM   #13
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Ohh, the comedy!

Evo2.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MD5 sum Pedroski Linux - Software 4 12-08-2009 02:17 PM
Why is the output says " this file has anunknown MD5 sum cc281828748e6fc9da3c68897321 judepeomichael Linux - Wireless Networking 2 06-27-2009 08:51 AM
Getting SUM in Bash thewebbie Programming 1 05-20-2009 11:42 AM
Sum of rows by formating output vimal480 Programming 8 09-03-2003 02:25 AM
Check sum emmanuelmathew Linux - Newbie 2 02-12-2003 01:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration