arithemetic problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers arithemetic problem
# 1  
Old 04-29-2006
arithemetic problem

[FONT=Courier New][FONT=Courier New]I am using a script which does a lot of math and I am facing a problem.One value is greater than 10 mi and the script converts it to exponents i.e 70000000 7e...
and this is causing my statements to become invalid.
This is what I used
A=`expr "${total}-${FAC}"|bc`

where $total and $FAC are variables and $total is the total which is got form an awk statement...
total=`awk '{tot=tot+$1} END {print tot}' "$filename"`

Could someone help me with this?
Thanks
# 2  
Old 04-29-2006
try formatted print in awk , something like
'{printf "%f\n",$tot}'

instead of jusr print.
# 3  
Old 04-29-2006
Hi Ahan,
I tried it but I got a syntax error .It defenitely is in the following statement:
total=`awk '{tot=tot+$1} END {print tot}' "$filename"`Any other suggestion?Thanks for the quick response
# 4  
Old 04-30-2006
similar issue

I had a similar issue. I wrote a small script to calculate total using 'for' loop and 'bc'.
for i in `list whatever column you want`
do
sum=$(echo "$sum + $i"|bc)
done
echo "Summed up value is $sum"

Regards,
Ranjith
# 5  
Old 04-30-2006
Quote:
Originally Posted by ranj@chn
I had a similar issue. I wrote a small script to calculate total using 'for' loop and 'bc'.
for i in `list whatever column you want`
do
sum=$(echo "$sum + $i"|bc)
done
echo "Summed up value is $sum"

Regards,
Ranjith
do this way ...

Code:
sum=0

for i in 1 2 3 4 5 6
do
echo $i
((sum=sum+i))
done
echo "Summed up value is $sum"

~
# 6  
Old 04-30-2006
not working

The change is not working when you have floating values. The 'bc' is taking care of the floating numbers but the ((sum=sum+i)) is not taking the floating values. instead it is truncating the values and summing them up.
# 7  
Old 04-30-2006
Hi
I tried using the for loop,but I need to find the sum of numbers in a file,called temp.
This file has approx 85000 numbers in one column and i used :

for i in temp
do
total=`expr "$total + $i"|bc`
done

I debugged the script and got a null value for the "total",
I even tried using
for i in ` cat temp` ....

it kind of worked but is taking a loong time.and the file is 174 KB in size.
Is there anyway to find the sum in a shorter time ?
Any help will be appreciated.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Router problem or ISP problem ?

Hi everyone, I am experiencing discontinuity of Internet service, this started 1 month ago. Everything worked very well for 1 year of intensive use, but now, I have problems reaching my gateway. The gateway is not my router but a node belonging to my ISP and I share the same public IP with... (3 Replies)
Discussion started by: remic
3 Replies

2. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

3. IP Networking

Problem with forwarding emails (SPF problem)

Hi, This is rather a question from a "user" than from a sys admin, but I think this forum is apropriate for the question. I have an adress with automatic email forwarding and for some senders (two hietherto), emails are bouncing. This has really created a lot of problems those two time so I... (0 Replies)
Discussion started by: carwe
0 Replies

4. AIX

AIX OS problem? network problem?

Dear ALL. I installed AIX OS on customer sites. but Only one site is too slow when I connected telnet, ftp.. Ping is too fast. but telnet and FTP is not connected.. of course i check the configuration file on aix but it's normal. Do any Idea?? thanks in advance. - Jun - (3 Replies)
Discussion started by: Jeon Jun Seok
3 Replies

5. Red Hat

Mail Problem. Maybe, it is a DNS Problem!

Hi, i've a redhat linux 9 upadated by redhat from 7 version to 9 version. A couple of days ago i was a problem with my mail, in other words i'm not able to get any email nor to send any email. I've a proxy configuration and i tried to set iptables in order to verify the port. The 110,255 and 995... (1 Reply)
Discussion started by: pintalgi
1 Replies

6. AIX

user login problem & Files listing problem.

1) when user login to the server the session got colosed. How will resolve? 2) While firing the command ls -l we are not able to see the any files in the director. but over all view the file system using the command df -g it is showing 91% used. what will be the problem? Thanks in advance. (1 Reply)
Discussion started by: pernasivam
1 Replies

7. Shell Programming and Scripting

ssh script problem problem

Hi Please help me with the following problem with my script. The following block of code is not repeating in the while loop and exiting after searching for first message. input_file ========== host001-01 host001-02 2008-07-23 13:02:04,651 ConnectionFactory - Setting session state... (2 Replies)
Discussion started by: pcjandyala
2 Replies

8. Shell Programming and Scripting

Problem in arithemetic operation

Hi All, I am facing a problem while doing arithemetic operation My code pasted below. COUNT_BREAKTIME=`expr ($COUNT_TOTALTIME1 - $COUNT_AVGTIME) / $COUNTER` error is ./OFR_Break_Avgtime.sh: command substitution: line 30: syntax error near unexpected token `$COUNT_TOTALTIME1'... (3 Replies)
Discussion started by: subin_bala
3 Replies

9. Shell Programming and Scripting

awk - arithemetic functions with external variables

I'm trying to get awk to do arithmetic functions with external variables and I'm getting an error that I cannot figure out how to fix. Insight would be appreciated money=$1 rate1=$(awk -F"\t " '/'$converting'/{print $3}' convert.table) rate2=$(awk -F"\t"... (2 Replies)
Discussion started by: DKNUCKLES
2 Replies

10. Shell Programming and Scripting

problem with dd command or maybe AFS problem

Hi, folks. Sorry for bothering, but maybe someone could help me please. The problem is the following: there is some script that copies files from local file system to AFS. The copying is performed with dd command. The script copies data into some AFS volumes. The problem appeared with one... (0 Replies)
Discussion started by: Anta
0 Replies
Login or Register to Ask a Question