The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep command nickg UNIX for Dummies Questions & Answers 14 4 Days Ago 08:22 AM
how to exclude the GREP command from GREP yamsin789 UNIX for Advanced & Expert Users 2 10-04-2007 11:59 PM
grep command help ishmael^soyuz Shell Programming and Scripting 4 07-11-2007 06:01 AM
grep command pmsuper UNIX for Dummies Questions & Answers 6 11-22-2006 03:12 AM
grep command debasis.mishra Shell Programming and Scripting 1 03-27-2006 10:53 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-25-2008
Registered User
 

Join Date: Apr 2008
Posts: 15
Stumble this Post!
grep command

how can i grep number in a file

if i want to grep 1234
can i use?

read inp1 inp2
echo inp1 > inp1.txt ;echo inp2 > inp2.txt
if inp=`grep -w [0-9] inp1.txt` -o inp1=`grep -w [0-9] inp2.txt`


thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-25-2008
Registered User
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 155
Stumble this Post!
You can directly grep the number.
Code:
grep -w 1234 filename
If you want to grep any 4 digit number, you can use
Code:
grep -w [0-9][0-9][0-9][0-9] filename
Reply With Quote
  #3 (permalink)  
Old 04-25-2008
Registered User
 

Join Date: Apr 2008
Posts: 15
Stumble this Post!
grep command

i know what you mean
but the problem is i do not know what the user will input
the user may input anything and i need to check if the input is number . else the error should be display
Reply With Quote
  #4 (permalink)  
Old 04-25-2008
Registered User
 

Join Date: Apr 2008
Posts: 19
Stumble this Post!
Numeric Check

As I know, there is no straight forward numeric check in shell script.
Through logic you can do.
--
typeset -i num
echo "Please enter string :"
read str
num=`expr $str + 1 2>/dev/null`
if [ $? -eq 0 ]
then
echo "Entered string is a number $str"
else
echo "Entered string is not a number $str"
fi
--
Hope it will help
Thanks
Sumit
Reply With Quote
  #5 (permalink)  
Old 04-29-2008
Registered User
 

Join Date: Apr 2008
Posts: 15
Stumble this Post!
grep command

hi,

is there any simpler code using grep command to test whether input is number or not?

thanks
Reply With Quote
  #6 (permalink)  
Old 04-29-2008
Registered User
 

Join Date: Apr 2008
Location: European Union/Germany
Posts: 183
Stumble this Post!
Be sure to add an extra check whether the user has given 0 as input when using sumitc's suggestion, which is the far better way to accomplish what you really want.

Generally one could render your idea in a more elegant way as follows
Code:
read value
value=`echo $value|grep -w [0-9]`
Reply With Quote
  #7 (permalink)  
Old 04-30-2008
Registered User
 

Join Date: Apr 2008
Posts: 15
Stumble this Post!
grep command

hi,
that does work but it doesnot support input more than one digit
if i want to enter number with 2 digits, the command does not work,
what should i alter to allow the code to read input of more than one digit?
thanks
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0