How to handle Numerical result out of range?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to handle Numerical result out of range?
# 1  
Old 04-23-2012
How to handle Numerical result out of range?

i have this code:

Code:
a=hello999999999999999999999999999999999999999999999999999999999
b=`echo ${a} | tr -d '[:alpha:]'`
if [ ${b} -le 0 ] ; then
    echo "Zero"
fi


but when I execute this I am having this error: Numerical result out of range
Anyone know how to handle this?


Thanks!!
# 2  
Old 04-23-2012
You could try something like:
Code:
a=hello999999999999999999999999999999999999999999999999999999999
b=`echo ${a} | awk 'gsub(/[a-zA-Z]/,x)'`
if [ "${b}" = "" ] ; then
    echo "Zero"
fi

# 3  
Old 04-23-2012
I would use something like this:

Code:
a=hello999999999999999999999999999999999999999999999999999999999
awk -v n="$a" 'BEGIN {
  gsub(/[^0-9]/, x, n)
  if (n == 0) print "Zero"
  }'

You could easily extend the code to handle negative and floating point numbers too:

Code:
a=hello-99999999999999999999999999999999999999999999999999999999.9
awk -v n="$a" 'BEGIN {
  gsub(/[^0-9.-]/, x, n)
  (n ~ /-/ && n ~! /^-/) && gsub(/-/, x, n)
  if (n <= 0) print "Zero"
  }'

And, of course, special care should be taken to handle strings like these:
Code:
hello-9-99
h.ello9


Last edited by radoulov; 04-23-2012 at 08:42 AM..
# 4  
Old 04-23-2012
Quote:
Originally Posted by Franklin52
You could try something like:
Code:
a=hello999999999999999999999999999999999999999999999999999999999
b=`echo ${a} | awk 'gsub(/[a-zA-Z]/,x)'`
if [ "${b}" = "" ] ; then
    echo "Zero"
fi

hmmm i need to compare the number so I cannot use

Code:
 if [ "${b}" = "" ] ; then

# 5  
Old 04-24-2012
Quote:
Originally Posted by h0ujun
hmmm i need to compare the number so I cannot use

Code:
 if [ "${b}" = "" ] ; then

Something like this?
Code:
if [ "${b}" = "" ] ; then
  echo "No numbers"
elif [ "${b}" -le 0 ] ; then
  echo "Less or equal to 0"
elif [ "${b}" -gt 0 ] ; then
  echo "Greater then 0"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare the current result with previous line result.?

Hi Gurus, I have requirement to compare current result with previous reuslt. The sample case is below. 1 job1 1 1 job2 2 1 job3 3 2 job_a1 1 2 job_a2 2 2 job_a3 3 3 job_b1 1 3 job_b2 2 for above sample file, GID is group ID, for input line, the job run... (1 Reply)
Discussion started by: ken6503
1 Replies

2. UNIX for Dummies Questions & Answers

Validate numerical

friends that way I can validate that the value is numeric taken instance var = 12re if var = numeric then a echo "Numerical else echo "not mumerico" fi edit by bakunin: please use CODE-tags for code, data and terminal output (yourself). (9 Replies)
Discussion started by: tricampeon81
9 Replies

3. UNIX for Dummies Questions & Answers

Difference between handle to the thread HANDLE and thread identifier pthread_t

This question might be silly but its confusing me a bit: What is the difference between handle to the thread HANDLE and thread identifier pthread_t? ---------- Post updated at 01:52 PM ---------- Previous update was at 01:48 PM ---------- Sorry I saw details and HANDLE is in windows and... (0 Replies)
Discussion started by: rupeshkp728
0 Replies

4. Shell Programming and Scripting

if [ "variable" = "numerical-range" ]; then

been a while so i'm a bit rusty and need a little help. writing a script that needs to compare $EXECHOST(a number) against a numerical range and then set a value. below isn't working but should give you folks an idea of my goal: if ; then echo "This is a 32B machine, exiting..." if ;... (4 Replies)
Discussion started by: crimso
4 Replies

5. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies

6. UNIX for Dummies Questions & Answers

how to get the value of a numerical expression

Hi, All, I want to calculate a specific value of a Gaussian distribution, say the mean is a=3, variance is b=5, the indepentent variable is x=2, how could I get the y which is the Gaussian distribution value of x. Thanks, Jenny (1 Reply)
Discussion started by: Jenny.palmy
1 Replies

7. UNIX for Dummies Questions & Answers

display the result of wc -l with words before and after the result

hello showrev -p | wc -l returns: 381 What to do in case I want to have this output: number of lines returned by showrev -p is: 381 thx (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

8. Shell Programming and Scripting

Outputting formatted Result log file from old 30000 lines result log<help required>

Well I have a 3000 lines result log file that contains all the machine data when it does the testing... It has 3 different section that i am intrsted in 1) starting with "20071126 11:11:11 Machine Header 1" 1000 lines... "End machine header 1" 2) starting with "20071126 12:12:12 Machine... (5 Replies)
Discussion started by: vikas.iet
5 Replies

9. UNIX for Dummies Questions & Answers

grep to handle a 0 result

Hi guys, I have the following grep command in a script to search through a file for a string and return its count, and it works fine for when the string exists: grep "string" file.txt | wc However, sometimes the result will be 0 and I want the script to take this as the result. Right now... (6 Replies)
Discussion started by: ocelot
6 Replies

10. Shell Programming and Scripting

Numerical Decision

I'm tryning to do something like this, I have this file: spaces12tabgoodbye spaces3tabhello I want to copy to another file the lines that have the number above 10... I thought using sort -rn but I don't know how to discard the lines that have the number below 10. Any idea? Thanks (3 Replies)
Discussion started by: pmpx
3 Replies
Login or Register to Ask a Question