1833742 Members
2452 Online
110063 Solutions
New Discussion

Testing for numbers

 
Pando
Regular Advisor

Testing for numbers

I have a lotid inside a file with the following patterns
....
....
DIFFLOTID, 1234
....
....

where 1234 is the lotid.
I would like to ask, how can i test it if the lotid contains other character other than numbers.
Maximum points to all correct replies.
5 REPLIES 5
Michael Roberts_3
Honored Contributor

Re: Testing for numbers

awk -F"," {if ($2 !~ /[[:alpha:]]/ )
print $0" OK lotid"
else
print $0" bad lotid"
' fileWithLotids
etouq ot hguone revelc ton m'i
john korterman
Honored Contributor

Re: Testing for numbers

Hi,
another possibility is to read the field and delete all numeric charecters from it to check if something still remains - as in the attached script, which can be run like this:
# attached.sh

regards,
John K.
it would be nice if you always got a second chance
Leif Halvarsson_2
Honored Contributor

Re: Testing for numbers

Hi,
Another solution is to assign the value to a integer variable (typeset -i . If the value is not an integer you get an error message and $? is set to 1.
Pando
Regular Advisor

Re: Testing for numbers

Hello John and Michael,

Thanks for the replies. My querries are, test 1234 as numeric or alphanumeric. The field DIFFLOTID either contains 1234 or 1234ver value. The reason i would like to test the value is because most value should contain numerics only but somehow i saw some value with alphanumerics e.g., 1234a, cor_1234 etc..
I currently get the value of DIFFLOTID using the command below:

Var=$(awk -F "," '/DIFFLOTID/ {print $2}' $FILE)

The $Var now contains the value and i need to test it if $Var is numeric or alphanumeric.
If the $Var is alphanumeric, i need to move it to a temporary directory.

Many thanks for your help.
Bharat Katkar
Honored Contributor

Re: Testing for numbers

Hi,
var1=1
((var2=var/var1))
if [ $? -ne 0 ]
then
echo " It's a Alphanumeric Character"
else
echo "it's a numeric character "
fi

Hope that works.
Regards,
You need to know a lot to actually know how little you know