Regex in if-then-else statement to match strings


 
Thread Tools Search this Thread
# 1  
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  
One possible solution:

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

# 3  
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  
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  
Using a case/esac statement makes regular expressions simple and obvious.

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

# 6  
Hi.
Quote:
Originally Posted by porter
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: https://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  
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

 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #143
Difficulty: Easy
Apple released a Unix system called Darwin in 2000 which became the core of the Mac OS X operating system, which was later renamed macOS.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

3. Shell Programming and Scripting

Using BASH =~ regex to match multiple strings

I have a scripting problem that I'm trying to solve, whereby I want to match that a string contains either of three strings. I'm thinking this is probably just me not understanding how to craft the appropriate regex. However, here's what I would like to do: ] && do-something more... (10 Replies)
Discussion started by: forrie
10 Replies

4. Shell Programming and Scripting

Perl - what does this statement mean -Regex

push @MACARRAY, "$+{catalog} $+{machine}\n" if ($info =~ /(?<catalog>catalog).+?(?<machine>\*+)/ms); I am (still) trying to solve problem. Looking around on the server I found this piece of code. Specifically what does "$+{catalog} $+{machine}\n" do ? Thanks in advance (1 Reply)
Discussion started by: popeye
1 Replies

5. Shell Programming and Scripting

Only Regex pattern match help

Hi We have a tool to monitor logs in our environment. The tool accepts log pattern match only using regex and I accept I am a n00b in that:confused:. I had been banging my head to make it work without much success and at last had to turn on to my last option to post it here. I had got great... (2 Replies)
Discussion started by: radioactive9
2 Replies

6. Shell Programming and Scripting

Regex within IF statement in awk

Hello to all, I have: X="string 1-" Y="-string 2" Z="string 1-20-string 2"In the position of the number 20 could be different numbers, but I'm interest only when the number is 15, 20,45 or 70. I want to include an IF within an awk code with a regex in the following way. ... (12 Replies)
Discussion started by: Ophiuchus
12 Replies

7. Shell Programming and Scripting

If statement with [[ ]] and regex not working as expected

Using BASH: $ if -- ::00" ]]; then echo "true"; else echo "false"; fi false Mike (5 Replies)
Discussion started by: Michael Stora
5 Replies

8. Shell Programming and Scripting

Regex: Get the word before match

Hi Input: MYTEXT.aa.bb cc.MYTEXT.aa.bb ee.dd.cc.MYTEXT.aa.bb cc.NOTEXT.a.b Output: <empty> cc cc <empty> I would like to use a regex to extract the last word before MYTEXT without the dot (2 Replies)
Discussion started by: chitech
2 Replies

9. UNIX for Advanced & Expert Users

Regex to match IP address

What do you think of this regex to match IP address? I have been reading up on regex and have seen some really long ones for IP. Would this fail in any scenarios? (+\.){3}* (5 Replies)
Discussion started by: glev2005
5 Replies

10. Shell Programming and Scripting

regex to match basename

Hi Can somebody please help me know how do i match the basename using a regular expression using posix standard in shell script suppose i want to match /u01/Sybase/data/master.dbf the result should be master.dbf as i want to match everything after the last / regards (8 Replies)
Discussion started by: xiamin
8 Replies

Featured Tech Videos