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
If statement - How to write a null statement april Shell Programming and Scripting 3 04-16-2008 02:14 PM
How to match the last XML extension by using Case statement sunitachoudhury Shell Programming and Scripting 3 04-06-2008 02:19 AM
if statement to match * sivasenthil_k UNIX for Dummies Questions & Answers 3 09-20-2006 12:39 PM
How to concatenate two strings or several strings into one string in B-shell? fontana Shell Programming and Scripting 2 08-26-2005 12:58 PM
sed regex Shakey21 UNIX for Dummies Questions & Answers 4 01-31-2002 09:16 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-18-2007
rakeshou rakeshou is offline
Registered User
  
 

Join Date: May 2007
Posts: 75
Regex in if-then-else statement to match strings

hello

I want to do a pattern match for string in the if statement, but I am not sure how to use regex inside the if statement.

I am looking for something like this:


Code:
if [ $name = [ab]{2,3} ]; then
.....
....
...
fi

  #2 (permalink)  
Old 09-18-2007
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 441
One possible solution:


Code:
if echo $name | grep -Eq '[ab]{2,3}'
then
    echo 'Yesss!'
fi

  #3 (permalink)  
Old 09-18-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 717
Hi.

If you can use bash3, then the "=~" operator and "[[" "]]" can be used with regular expressions.

However, a method that would work with all Bourne-family shells would be:

Code:
if echo "$VARIABLE" | grep "regular-expression"
then
  echo Success
else
  echo Failure
fi

Your version of grep may or may not allow the repeat count, GNU grep does.

See man bash for details on the former ... cheers, drl
  #4 (permalink)  
Old 09-19-2007
rakeshou rakeshou is offline
Registered User
  
 

Join Date: May 2007
Posts: 75
but this prints the grep result. I tried to redirect the output to /dev/null but then if fails. Is there a way I can avoid the grep result on stdout?
  #5 (permalink)  
Old 09-19-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Using a case/esac statement makes regular expressions simple and obvious.


Code:
case $name in
    [ab]{2,3} )
         echo do stuff
         ;;
    * ) 
         ;;
esac

  #6 (permalink)  
Old 09-19-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 717
Hi.
Quote:
Originally Posted by porter View Post
Using a case/esac statement makes regular expressions simple and obvious.


Code:
case $name in
    [ab]{2,3} )
         echo do stuff
         ;;
    * ) 
         ;;
esac
Perhaps I am using a too-old version of bash. Version 3.00.16(1) recognizes the [ab] but not the {2,3} part, apparently because the case selectors are expanded in the same fashion as pathnames, not regular expressions:

Code:
#!/bin/bash3

# @(#) s4       Demonstrate case selectors.

set -o nounset
echo

echo "GNU bash $BASH_VERSION" >&2
echo " MUST USE VERSION 3 FOR REGULAR EXPRESSIONS WITH =~ OPERATOR!"
# See: http://www.unix.com/showthread.php?p=302136557&posted=1#post302136557

echo

# if [ $name = [ab]{2,3} ]; then
name="b"
name="bb"
echo " Original string = \"$name\""

case $name in
[ab]{2,3} )
    echo Success
        ;;
* )
    echo Failure
        ;;
esac

echo
name="b{2,3}"
echo " Original string = \"$name\""

case $name in
[ab]{2,3} )
    echo Success
        ;;
* )
    echo Failure
        ;;
esac

exit 0

producing:

Code:
% ./s4

GNU bash 3.00.16(1)-release
 MUST USE VERSION 3 FOR REGULAR EXPRESSIONS WITH =~ OPERATOR!

 Original string = "bb"
Failure

 Original string = "b{2,3}"
Success

... cheers, drl
  #7 (permalink)  
Old 09-20-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,928
Yes, it's globbing, not re's.
For {2,3} with globbing:


Code:
bash 3.2.25(1)$ v=b
bash 3.2.25(1)$ case $v in ([ab][ab]|[ab][ab][ab]) echo OK;;(?) echo KO;;esac
KO
bash 3.2.25(1)$ v=bbb
bash 3.2.25(1)$ case $v in ([ab][ab]|[ab][ab][ab]) echo OK;;(?) echo KO;;esac
OK
bash 3.2.25(1)$ # or:
bash 3.2.25(1)$ shopt -s extglob
bash 3.2.25(1)$ v=b
bash 3.2.25(1)$ case $v in ([ab][ab]?([ab])) echo OK;;(?) echo KO;;esac
KO
bash 3.2.25(1)$ v=bb
bash 3.2.25(1)$ case $v in ([ab][ab]?([ab])) echo OK;;(?) echo KO;;esac
OK

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 06:24 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