The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 10-06-2008 11:22 AM
how to exclude the GREP command from GREP yamsin789 UNIX for Advanced & Expert Users 2 10-05-2007 02:59 AM
grep command help ishmael^soyuz Shell Programming and Scripting 4 07-11-2007 09:01 AM
grep command pmsuper UNIX for Dummies Questions & Answers 6 11-22-2006 07:12 AM
grep command debasis.mishra Shell Programming and Scripting 1 03-28-2006 01:53 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-25-2008
christine33990 christine33990 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 15
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
  #2 (permalink)  
Old 04-25-2008
krishmaths krishmaths is offline
Registered User
  
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 191
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
  #3 (permalink)  
Old 04-25-2008
christine33990 christine33990 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 15
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
  #4 (permalink)  
Old 04-25-2008
sumitc sumitc is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 25
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
  #5 (permalink)  
Old 04-29-2008
christine33990 christine33990 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 15
grep command

hi,

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

thanks
  #6 (permalink)  
Old 04-29-2008
fabtagon fabtagon is offline
Registered User
  
 

Join Date: Apr 2008
Location: European Union/Germany
Posts: 189
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]`
  #7 (permalink)  
Old 05-01-2008
christine33990 christine33990 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 15
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0