How to check whether a string is number or not


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check whether a string is number or not
# 8  
Old 09-21-2005
The problem can easily be solved by considering what a "number" is actually and since this is a good opportunity to show how to develop regular expressions I will explain that in greater detail:

1st Try
The only characters allowed should be 0-9. This covers for "123", but not for "123.0". Hence

2nd Try
Only 0-9 are allowed, save for exactly 1 character, which may be a fullstop. This definition covers "123" and "456.78", leaves out (quite correctly) "123.456.789", but doesn't cover "-123.0" or "+123.0". Hence

3rd Try
In addition to 2nd Try the first character may be "+" or "-" optionally. This covers every number and leaves out any non-number, so this solution is perfect.

We develop a simple sed-statement from there, by consecutively throwing out all "allowed" chars and look, if something remains. If something remains it is non-numeric, if not, then it is numeric:

Code:
if [ -n "$( print - "$VarInQuestion"           |\
            sed 's/^[+-]//;s/[0-9]//g;s/\.//'   \
          )" ] ; then
     print - "$VarInQuestion is non-numeric"
else
     print - "$VarInQuestion is numeric"
fi

Here is the sed-statement in detail:

s/^[+-]// - see 3rd try, we chop off leading +/- signs
s/[0-9]//g - see 1st try, we throw out all numerical digits
s/\.// - see 2nd try, we throw out one possible decimal point


bakunin
# 9  
Old 09-21-2005
Hi All..
Thanks a lot....
It is working fine now...
Shihab
# 10  
Old 04-10-2009
Code:
$
$ x=1; if [ "`expr $x - $x 2>/dev/null`" == "0" ]; then echo is number; else echo not number; fi
is number
$ x=-1; if [ "`expr $x - $x 2>/dev/null`" == "0" ]; then echo is number; else echo not number; fi
is number
$ x=a; if [ "`expr $x - $x 2>/dev/null`" == "0" ]; then echo is number; else echo not number; fi
not number
$ x=1a; if [ "`expr $x - $x 2>/dev/null`" == "0" ]; then echo is number; else echo not number; fi
not number
$

# 11  
Old 04-27-2009
This solution is better as it works for decimals as well:

Code:
$ x=a; printf "%g" $x >/dev/null 2>&1; if [ $? -eq 0 ]; then echo is number; else echo not number; fi
not number
$ x=9a; printf "%g" $x >/dev/null 2>&1; if [ $? -eq 0 ]; then echo is number; else echo not number; fi
not number
$ x=9; printf "%g" $x >/dev/null 2>&1; if [ $? -eq 0 ]; then echo is number; else echo not number; fi
is number
$ x=9.1; printf "%g" $x >/dev/null 2>&1; if [ $? -eq 0 ]; then echo is number; else echo not number; fi
is number
$ x=-9.1; printf "%g" $x >/dev/null 2>&1; if [ $? -eq 0 ]; then echo is number; else echo not number; fi
is number
$ x=-9; printf "%g" $x >/dev/null 2>&1; if [ $? -eq 0 ]; then echo is number; else echo not number; fi
is number

# 12  
Old 04-27-2009
Code:
awk '$1+0==$1' a.txt

# 13  
Old 04-28-2009
try this:

Code:
function validatenumber
{
number="$1"
if [ -z $number ];then
echo "not  something" > &2;return 1
fi

if [ "${number%${number#?}} = "-" ]; then
testvalue="${number#?}"
fi

if [ "${number%${number#?}} = "+" ]; then
testvalue="${number#?}"
fi

nodigits="$(echo $testvalue | sed 's/[[:digit:]]]//g')"
if [ ! -z $nodigits ]; then
echo "Invalid Number" > &2
return 1
fi

return 0
}

if validatenumber "$1"; then
echo "No is a valid integer"
fi


cheers,
Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

3. Shell Programming and Scripting

Check parameter is number or string

Hey I'm new in linux, I'm looking for a code to check whether the parameter is a number or a string. I have already tried this code: eerste=$(echo $1 | grep "^*$">aux) if But it doesn't work.:confused: Thanks (2 Replies)
Discussion started by: Eclecticaa
2 Replies

4. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

5. Shell Programming and Scripting

Check if the value is Number

Hi, I have a file with data as given $cat file1.txt 123 234 23e 234.456 234.876e 345.00 I am checking if the values are proper integers using the command. nawk -F'|' 'int($1)!=$1 {printf "Error in field 1|"$0"\n"}' file1.txt This is checking for only integers ( without... (10 Replies)
Discussion started by: ashwin3086
10 Replies

6. Shell Programming and Scripting

How to Check given string is number in shell script?

Hi, Can anyone help me out to check whether the input argument is number? Example: REQUEST_ID="123456" I need to check the REQUEST_ID value is number or string. Thanks in Advance Regards BS (6 Replies)
Discussion started by: balajiora
6 Replies

7. Shell Programming and Scripting

how to search string and number in one file and check in the other file

Hi, Can anyone help in the below problem. file1 has the below contents fileset 999 primary-ilist inode 37020 has invalid dotdot (426094) -> Not exist fileset 999 primary-ilist inode 115016 dup block ->... (9 Replies)
Discussion started by: knshree
9 Replies

8. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

9. UNIX for Dummies Questions & Answers

check whether variable number or string

I am passing an argument to a file and i wanna check whether the argument is a number or string ? how can i do this? (4 Replies)
Discussion started by: rolex.mp
4 Replies
Login or Register to Ask a Question