Help In Calculation of large values in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help In Calculation of large values in loop
# 1  
Old 09-12-2007
Help In Calculation of large values in loop

Hi Gurus,

I am writing a shell script in which i need to strip out the numbers from file
the values are unknown i. e. the range cannot be predicted.. and in my current program the sum of values is not coming as desired i think the value of calculation is crossing the range i.e. after some addition it starts to become negative and the results are wrong.

values for e.g are as follows
302565658945
302565658945
565658945589
55658945589
56565894558912

The sum should be for all of above values.

So On..
Please help me as i have tried let, expr. and being a new shell programmer i am stuck up here.

Thanks in advance

sandeep
# 2  
Old 09-12-2007
if you need to sum up all the numbers in the file 'myFile.txt':
Code:
echo `sed -e 's/$/+/' myFile.txt` 0 | bc

# 3  
Old 09-12-2007
Help In Calculation of large values in loop

Thanks for reply

The numbers i need to pick up are decided based on certain beginning value
of a comma separated values and only for specific rows in a file, i am able to get these values stripped out in a separate file, only the calcualtion is coming wrong. Say a.txt is a file and following are the contents :

12345678912
123458785454
145645645454
4548778545487
545645
79879878978787878


I am opening this file to perform addition in loop.

Pls. comment
Sandeep
# 4  
Old 09-12-2007
Quote:
Originally Posted by sandeepb
Thanks for reply

The numbers i need to pick up are decided based on certain beginning value
of a comma separated values and only for specific rows in a file, i am able to get these values stripped out in a separate file, only the calcualtion is coming wrong. Say a.txt is a file and following are the contents :

12345678912
123458785454
145645645454
4548778545487
545645
79879878978787878


I am opening this file to perform addition in loop.

Pls. comment
Sandeep
so does the proposed solution work on the the 'a.txt' file or not?
I get a value of '79884709207988830' for the posted sample a.txt

If you do your 'stripping' [no pun inteded] with 'awk', you might be able to do your additions in the same awk script with no need for the intermediate 'stripped' file.
# 5  
Old 09-12-2007
Help In Calculation of large values in loop

Hi,

Thanks for your help. I will need to check it as i don't have MKS Toolkit or Linux os with me right now, I will work on your proposed solution and get back to you tomm.

Thanks
Sandeep
# 6  
Old 09-13-2007
Your suggested solution has worked fine. Thanks a lot
Just 1 more question how can i store the output in variable instead of echo ? for same command

Thanks
Sandeep
# 7  
Old 09-13-2007
Quote:
Originally Posted by sandeepb
Your suggested solution has worked fine. Thanks a lot
Just 1 more question how can i store the output in variable instead of echo ? for same command

Thanks
Sandeep
depends on a shell. for ksh:
Code:
#!/bin/ksh

myVar=$(echo `sed -e 's/$/+/' myFile.txt` 0 | bc)
echo "myTotal -> [${myVar}]"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reoccuring peak values in large data file and print the line..

Hi i have some large data files that contain several fields and rows the data in a field have a numeric value that is in a sine wave pattern what i would like todo is locate each peak and pick the highest value and print that complete line. the data looks something like this it is field nr4 which... (4 Replies)
Discussion started by: ninjaunx
4 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

Reading off values from a large file

Hi, I have a large output file (star.log), with many lines of the following type *** T vavg unburnt: 723.187 / burnt: 2662.000 What I would like to do is pick the values 723.187 and 2662.000 and What I've got so far is awk '/unburnt:.*burnt:/{Tu=$6;Tb=$NF}END{print Tu, Tb}'... (6 Replies)
Discussion started by: lost.identity
6 Replies

4. Shell Programming and Scripting

How to get full width of screen, when using large calculation with bc ?

Experts, When doing large calculation the digits in the screen are limiting to 68 digit and then with a \ ( backspace) next line comes. example: ubuntu# echo "123456789 ^ 50 " | bc 37648602365872212683379005814670372328125515868188009630652959693316\... (4 Replies)
Discussion started by: rveri
4 Replies

5. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

6. Shell Programming and Scripting

How to remove a subset of data from a large dataset based on values on one line

Hello. I was wondering if anyone could help. I have a file containing a large table in the format: marker1 marker2 marker3 marker4 position1 position2 position3 position4 genotype1 genotype2 genotype3 genotype4 with marker being a name, position a numeric... (2 Replies)
Discussion started by: davegen
2 Replies

7. Shell Programming and Scripting

Comparison between 2 large lists with Getting VALUES from one into the other

hi, I have 2 large lists: LIST A: containes 6 fields of many entries (VARIABLE number), like: 2011-07-10 | 18:19:47 | 38037300 | 9647808003122 | 2 | success LIST B: containes 3 fields & 183 entries (FIXED number), like: 9647805651885 9647805651885 SCP_10 What I want is a... (8 Replies)
Discussion started by: amurib
8 Replies

8. UNIX for Dummies Questions & Answers

calculation on a large file

Trying to do some simple maths on a large file. Excel works fine but I have 1 million entries: If the difference between a number in column 2 and the one above it is more than 100 the insert a new line 8 4001 4100 8 4101 4200 8 4201 4300 8 15901 16000 8 15910 ... (1 Reply)
Discussion started by: dr_sabz
1 Replies

9. Shell Programming and Scripting

Avoid "++ requires lvalue" Error in Loop Calculation

Hi All, Please help me to perform sum of values in a loop, I am getting following error: "total=0++432907765772: ++ requires lvalue" where actual statement is as : total=$total+$amt where amt can have +ve or -ve values Thanks Sandeepb (3 Replies)
Discussion started by: sandeepb
3 Replies

10. Shell Programming and Scripting

How to add two large values

Hi, Gives me wrong value when, $ echo `expr 2221753117 + 299363384` -1773850795 How to overcome this? Appreciate any help on this. -Om (5 Replies)
Discussion started by: Omkumar
5 Replies
Login or Register to Ask a Question