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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
cat operation trichyselva UNIX for Dummies Questions & Answers 0 03-24-2008 06:55 AM
Array operation phamp008 Shell Programming and Scripting 3 01-18-2008 05:31 AM
Help with arithmetic operation emjayshaikh Shell Programming and Scripting 3 09-23-2007 11:44 PM
RPC failure on yp operation Remi SUN Solaris 1 05-22-2007 10:15 AM
AIX 4.3 Openssh 3.7.1.0 Operation ripley AIX 2 03-07-2005 01:56 PM

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 05-15-2006
burakkilic burakkilic is offline
Registered User
  
 

Join Date: May 2006
Posts: 6
Problems with sum operation

i wrote this code..

#!/bin/sh
sum=0
for i in `cat numbers.txt |cut -f1`
do
sum=expr $sum + $i
done
echo $sum


I want to read the numbers in the file numbers.txt. and find sum of them. But this code only writes the numbers(without making sum) that it reads. Help me..
  #2 (permalink)  
Old 05-15-2006
steweston steweston is offline
Registered User
  
 

Join Date: May 2006
Location: Vancouver, BC
Posts: 11
Try a bit of awk

burakkilic,

I normally use awk for this kind of problem - it works effectively

awk '{tot+=$1} END{print tot}' numbers.txt

Alternatively, change your script as shown:

#!/bin/sh
sum=0
for i in `cat numbers.txt |cut -f1`
do
sum=`expr $sum + $i` # Add back quotes around the expr
done
echo $sum


Steve
  #3 (permalink)  
Old 05-15-2006
burakkilic burakkilic is offline
Registered User
  
 

Join Date: May 2006
Posts: 6
Thank you very much!.. The code is working properly
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 09:12 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