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
TEST command amatuer_lee_3 Shell Programming and Scripting 4 05-09-2008 07:36 AM
Using grep in a test/if statement cbo0485 Shell Programming and Scripting 4 03-21-2008 12:29 AM
Test command JWilliams AIX 2 12-04-2007 02:53 AM
the TEST command marwan UNIX for Dummies Questions & Answers 2 04-23-2007 06:51 PM
unix grep command pmsuper Shell Programming and Scripting 1 09-19-2006 02:06 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-08-2007
netmaster netmaster is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 6
Unix grep/test command

Hello, i have a script which checks if the user entered 8 numeric characters in the form of YYYYMMDD (birth date). If the user entered any non numeric characters, an error will be displayed:

Code:


# Check to see if the 8 characters are all numbers
# If not show error essage
# And prompt user for more input

         echo $char | grep -q '^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$'  
if 	[ $? -ne 0 ]
then
         echo "You have entered non-numeric values.  Please type in the form of YYYYMMDD"
	read char
        continue

Is there a simpler way to write this command without using the [0-9] value for each field? Any help would be appreciated
  #2 (permalink)  
Old 10-09-2007
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
On Solaris you can use:

Code:
echo "12345678" | /usr/xpg4/bin/grep -Eq "^[0-9]{8}$"
In any case, you must use a tool which supports extended regular expressions to use the above regex.
  #3 (permalink)  
Old 05-07-2009
12345 12345 is offline
Registered User
  
 

Join Date: May 2009
Posts: 8
Comma delimited instead of space delimited

Hi,

What if the numbers are comma separated and the no.of occurences of the pattern is not known.
E.g. 1,2,3,4,...n

TIA
  #4 (permalink)  
Old 05-07-2009
thegeek thegeek is offline
Registered User
  
 

Join Date: Apr 2009
Location: /usr/bin/vim
Posts: 331
@12345 If i understood your question correctly, then the regex is:
Code:
grep -E "^[0-9,]{3,}"
  #5 (permalink)  
Old 05-08-2009
12345 12345 is offline
Registered User
  
 

Join Date: May 2009
Posts: 8
regular expression for Comma separated numbers

Hi,

Thanks for your reply. I just want to make this requirement more clear.
I read values to a variable, using "read var".I am now validating the user input. The user should input the values in the format 1,3,6 (can enter any numeric values upto n).
I have tried this for 2 numbers. This is the command that I had used.
read col
1,2 ----> input provided by user
echo $col | grep -w "^[0-9],[0-9]$"
return code is successful.
If it contains any other input like 1,a or a,1 or a,b it returns a code of 1.

This code works fine for 2 numbers, but I want to make this work for 'n' numbers.
I had tried the solution provided by you, but -E command is not recognised by my script.
TIA
Sponsored Links
Closed Thread

Bookmarks

Tags
regex, regular expressions

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 08:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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