Operating System - Linux
1752777 Members
6499 Online
108789 Solutions
New Discussion

How to check whether a field is numeric

 
James R. Ferguson
Acclaimed Contributor

Re: How to check whether a field is numeric

Hi Pavitra:

I wholly second Hein's remarks regarding Perl.

That said, here's yet another way using 'expr'. The idea is to determine if a field consists only of numbers for the length of the field. By example.

#!/usr/bin/sh

F=`echo "111,222,333,444,5X5,666"|cut -d, -f4`
[ `expr "${F}" : '[0-9]*'` -eq `expr "${F}" : '.*'` ] \
&& echo "numeric" || echo "not numeric"

F=`echo "111,222,333,444,5X5,666"|cut -d, -f5`
[ `expr "${F}" : '[0-9]*'` -eq `expr "${F}" : '.*'` ] \
&& echo "numeric" || echo "not numeric"

exit 0

Regards!

...JRF...