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
Unix:mail sending issue..pls help me soon bobprabhu UNIX for Dummies Questions & Answers 2 02-01-2008 05:44 AM
Unix Login Issue freakygs UNIX for Advanced & Expert Users 1 01-16-2008 08:21 AM
Issue with Unix cat command RcR Shell Programming and Scripting 13 10-31-2007 05:54 AM
Unix command mmin issue Mohee UNIX for Dummies Questions & Answers 1 08-30-2007 10:41 PM
Unix in a Windows environment issue Aisha Sturkey UNIX for Dummies Questions & Answers 6 05-02-2005 09:44 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-12-2008
thambi thambi is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 32
Question Unix Arithmatic operation issue , datatype issue

Hi,
I have a shell scripting. This will take 7 digit number in each line and add 7 digit number with next subsequent lines ( normal addition ).

Eg:

0000001
0000220
0001235
0000022
0000023
...........
.........
........


Like this i am having around 1500000 records. After adding , I am getting the result 2147483647 but actual result is 2156379608 . I found the root cause of this issue is that Unix temproary variable can hold only 2156379608 ( Range of integer ) as it's 32 bit. For eg:

a=2147483647
expr $a + 2

you will get the negative result. Because while expr working, the result will be stored in internal temporary variable or register then you will get the result.. but that particular temporary variable or register can accomodate only 2147483647 .. if it crosses this limit, you may get the junk value like -ve values... this is my finiding for this issue.. But I want to have resolution for this issue. how to add or do arthimatic opration if i want to have result more than limit ( 2147483647 ). I mean

a=2147483647
expr $a + 2

for this i need to get the right result rather than -ve value.
  #2 (permalink)  
Old 02-12-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
This will handle up to 15 signifcant digits:
Code:
 awk ' {total+=$0} END { print total} ' file
This will handle bigger numbers depending on your version of bc:
Code:
#/bin/ksh
total=0
while read value
do
    total=$(echo "$value + $total" | bc )
done < file
echo $total
  #3 (permalink)  
Old 02-13-2008
thambi thambi is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 32
Question Reply

Hi ,

Thanks. We are also using the awk for arthitmatic operation . But we are not getting exepected result. I tried with your code as well.

$ awk ' {total+=$0} END { print total} ' hash_column_wrong.txt
2.15638e+09

I got the result in expontial format. But I need the result with all the digits. How to achive this?. Then second method which you shown using the bc calculator is taking more time to complte. So I think awk is the best method. As of now , I am getting result as 2.15638e+09 . but I need to get result like 2156380000 . Please help me.

Regards,
Thambi
  #4 (permalink)  
Old 02-13-2008
manas_ranjan's Avatar
manas_ranjan manas_ranjan is offline
Registered User
  
 

Join Date: Jul 2007
Location: Amsterdam
Posts: 177
can you please provide the list of hash_column_wrong.txt ????
  #5 (permalink)  
Old 02-13-2008
thambi thambi is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 32
Reply..

As I already mentioned, it just contains the 7 digiti number in every row. Like these, this file has around 150000 . We just need to add all the rows..that's all

0000001
0000343
0000001
0001426
0000001
0000718
0000162
0000103
0000021
0000011
0000016
0000312
0000026
0000031
0000005
0000022
0000001
0000001
0011845
0003713
0000001
0000011
0000001
0000001
0000001
..,.,
........
.......
  #6 (permalink)  
Old 02-13-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,864
Use awk to prepare the file for bc.

{ awk '{ print $0 " +\\" }' bigfile; echo 0; } | bc

Assuming bc is using the arbitrary precision library, you shouldn't have a problem here.
  #7 (permalink)  
Old 02-13-2008
thambi thambi is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 32
Reply....

When I run the below command

awk '{ print $0 " +\\" }' hash_column_wrong.txt | bc

I got below exception..

bundling space exceeded on line 1,
bundling space exceeded on line 1,
bundling space exceeded on line 1,

is it due to \n character that occurs end of the line?.
Sponsored Links
Closed Thread

Bookmarks

Tags
bc, big numbers, gawk, sum by column

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 08:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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