The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Date Calculations mitschcg UNIX for Dummies Questions & Answers 8 03-06-2009 04:40 AM
Time Calculations & Conversions Nysif Steve UNIX for Advanced & Expert Users 1 09-14-2007 11:07 AM
Time Calculations Nysif Steve UNIX for Dummies Questions & Answers 5 09-14-2007 08:49 AM
Non-integer calculations in bash jeriryan87 Shell Programming and Scripting 2 06-27-2007 04:17 PM
Float calculations sharmavr UNIX for Dummies Questions & Answers 1 07-26-2006 02:18 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-10-2008
nadman123 nadman123 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 23
how do you get input of a file to calculations

ex:-
say you have a file called numbers.txt . and in it you have the following numbers
12
75
74
24
83
how do you get the sum of all those numbers?
  #2 (permalink)  
Old 04-10-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by nadman123 View Post
ex:-
say you have a file called numbers.txt . and in it you have the following numbers
12
75
74
24
83
how do you get the sum of all those numbers?
This looks more like homework. If it is not homework, search the forum.

Last edited by vino; 04-10-2008 at 01:32 AM..
  #3 (permalink)  
Old 04-10-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,079
Code:
awk '{
 a=a+$0
 }
 END{
print a
}' filename
  #4 (permalink)  
Old 04-10-2008
tonan tonan is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 11
you can also create a shell script for this, using for.. this is the lenghty way
x=0
for i in `cat numbers.txt`
do
x=`expr $x + $i`
done
echo $x
  #5 (permalink)  
Old 04-10-2008
nadman123 nadman123 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 23
Smile

thanks guys you guys are awsome
  #6 (permalink)  
Old 04-11-2008
soemac soemac is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 12
Code:
$ cat filename | xargs -n5 | tr ' ' '+' | bc
268
  #7 (permalink)  
Old 04-11-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
That's a Useless Use of Cat, and xargs already knows how many arguments there are.

Code:
xargs <filename | tr ' ' + | bc
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0