Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am looking to do something like this in KSH:

if (( $var = (foo|bar)[0-9]*$ )); then
    print "variable matched regex"
fi

Is it possible at all?

For the record I'm using Ksh Version M-11/16/88i on a Solaris 10 machine.

share|improve this question
3  
Do you realize the regular expression [foo|bar] means "match a single character from the set (a,b,f,o,r,|)"? If you mean "match 'foo' or 'bar'" you want (foo|bar) –  glenn jackman Oct 19 '11 at 14:20
    
True, didn't notice that. I will update accordingly. –  rahmu Oct 19 '11 at 14:26

5 Answers 5

up vote 4 down vote accepted

Ksh has regular expressions, but not in the usual syntax (not in the version in Solaris 10).

if [[ $var = *@(foo|bar)*([0-9]) ]]; then …

In the manual, look under “conditional expressions” for what's inside the brackets and under “file name generation” for the pattern syntax.

share|improve this answer

Using case with glob patterns might work for you. The composite pattern *(pattern-list) means "Matches zero or more occurrences of the given patterns" and @(pattern-list) means "Matches exactly one of the given patterns."

matcher() {
  typeset var="$1"
  case "$var" in
    *@(foo|bar)*([0-9])) print "$var matched" ;;
    *) print "$var did not match" ;;
  esac
}

for var in foo bar baz foo123 abc_foo132 abc_foo123z bar1 1bar1 1bar1a; do 
  matcher "$var"
done

Outputs:

foo matched
bar matched
baz did not match
foo123 matched
abc_foo132 matched
abc_foo123z did not match
bar1 matched
1bar1 matched
1bar1a did not match
share|improve this answer

Why not use egrep(1)? Gives you all a regex user could wish for:

 if echo "$var" | egrep -s '(foo|bar)[0-9]*$'    # -s means "silent"
  then
    ...

Additional note for Solaris: With Solaris you may want to check the manpage for egrep - there is annother egrep version that is located at /usr/xpg4/bin/egrep that supports some more options and differs in functionality when it comes to advanced regex stuff.

share|improve this answer
1  
sometimes Solaris will have GNU Egrep installed as well, as gegrep. Either from the Companion CD, or from SunFreeWare, and usually in /opt/sfw/bin or /usr/local/bin. –  Tim Kennedy Oct 20 '11 at 19:52
    
@Tim: true, but I'd never rely on that. From my experience with customer production systems you'd better stick to what the base OS provides since what we would consider cool and helpful is often not allowed in production. +1 anyway ;-) –  ktf Oct 21 '11 at 11:50
    
100% agreed. i try to always use scripts/programs/perl modules that are installed by default. keeps things portable. –  Tim Kennedy Oct 22 '11 at 4:57

I did something like this, using sed. I don't know how good it is, but at least it worked ^^

if [ -z "$(echo "$var" | sed -e 's/(foo|bar)[0-9]*$//')" ]; then
    print "variable matched regex"
fi
share|improve this answer
    
You need to put double quotes around variable substitutions, as always (otherwise you'll get wrong results or errors on some inputs containing wildcard characters or whitespace). Even with the right quoting, this assumes that the input doesn't contain a newline. Furthermore, your method is highly convoluted; grep is more natural, but there's a way that's built into ksh. –  Gilles Oct 20 '11 at 16:47

I know I'm late to the party, but instead of ktf's solution:

 if echo "$var" | egrep -s '(foo|bar)[0-9]*$'    # -s means "silent"
  then
    ...

you may also use

 if grep -q '(foo|bar)[0-9]*$' <<< "$var"        # -q means "quiet" ;-)
  then
    ...

This technique with the <<< operator is called the herestring. However, I should also add a warning: this WON'T work under a read-only environment (i. e. you will get ugly error messages to stderr), since the herestring requires a temporary file to be written to somewhere. As far as I know, the "classic" way with echo and a pipe will also work without any write permission at all.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.