Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding up a list of numbers in KSH

Status
Not open for further replies.

AdamCoombs

Technical User
May 22, 2002
51
GB
Hello...

I need to be able to total up a column of numbers..
prices to be precise!

29.99
22.99
24.99
etc etc

dont care how we do it, i can pull the numbers out and put them in their own file for the adding up bit...!
HELP PLEASE!
cheers
Adam
 
Are you always going to have two decimal places?
 
yup, there prices...
if its just 23 pounds then i can force the user to enter 23.00 if need be!
 
I've got something that adds whole numbers, but ignores the numbers right of the decimal. I'm trying to figure a way to multiply the lines by 100, then add them and then divide the result by 100.

Here is what I have used successfully with files (with an extension of .num) that have lists of whole numbers:

for filename in *.num
do
typeset -i i
i=0
while read -r j
do
i=i+j
done < ${filename}
echo $i > ${filename}.total
done

I'll try to dig some more.
 
awk '{sum+=$1} END {print sum}' < $filename > $filename.total
 
# assuming one value per line
echo $(tr '\n' '+' < filaname.txt)0 | bc

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top