1752805 Members
5394 Online
108789 Solutions
New Discussion юеВ

Only integers

 
SOLVED
Go to solution
Praveen Bezawada
Respected Contributor

Only integers

Hi
I need to check that a particular field in comma separated file has only integers.
I have tried it using typeset as
typeset -i txt
while read each;do
txt=`cut -f, -d1`
done In this case if the field is non integer, the whole script fails.
I have also used grep as
while read each;do
echo $each| cut -f, -d1 | grep -v [0-9] > /dev/null
if [ $? -eq 0 ]; then
non integer
fi
done < filename
This works but is very slow.
Can anyone help me write a faster script...

Thanks
...BPK...
7 REPLIES 7
Sridhar Bhaskarla
Honored Contributor

Re: Only integers

Hi Praveen "Vijayawada",

Check this thread. We had a discussion on getting rid of this "typeset" problem.

http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0x235f1012aa92d5118ff10090279cd0f9,00.html

Sridhar
You may be disappointed if you fail, but you are doomed if you don't try
James R. Ferguson
Acclaimed Contributor
Solution

Re: Only integers

Hi Praveen:

Here's one way, where "X" contains the data of interest:

if [ `expr $X : '[0-9]*'` -ne `expr $X : '.*'` ]
then
echo "Integer expected!"
fi

Regards!

...JRF...
Sridhar Bhaskarla
Honored Contributor

Re: Only integers

And ofcourse JRF has the matured way of dealing it.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Praveen Bezawada
Respected Contributor

Re: Only integers

Hi
Thanks JRF, it is faster than what I had tried.

And Sridhar Congratulations on becoming a 'GRADUATE' so fast.... keep it up....
Mana Desam peruni veluguloki techaru kadaa, meru...

...BPK...
James R. Ferguson
Acclaimed Contributor

Re: Only integers

Hi Praveen & Sridhar:

As always, I'm glad to have helped, Praveen.

...and Sridhar, congratulations to you!

...JRF...
Sridhar Bhaskarla
Honored Contributor

Re: Only integers

Thank you JRF,

You are the inspiration for most of us.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Suhas_3
Advisor

Re: Only integers

Hi, did you try this ?

if grep "[^0-9,]" fil1 > /dev/null
then
echo non integer
fi

Regards,
Suhas