grep and regex question

basically i have a csv i parse through. a user will supply me with a san switch he/she wants more info about... say the name is "pnj-sansw124"

now i can grep out every connection to that switch w/o issue because this sans switch pnj-sansw124 has multiple slots 1-10. and it looks like this in the csv "pnj-sansw124-1/23" where 1/23 is slot/port. so...

switch=pnj-sansw124
slot=1

grep -w $switch"-"$slot $file1 | awk -F, '{ print $2,$7 }' | while read cl1 cl2
do
    .
    .
    .
done

this works just fine

however i run into a problem where there is a san switch with no slots. so i would like to grep with a regex that would allow me to look for the "pnj-sansw124" but look for the "-[0-9]/" extension as well. so if its there, then continue to ask for slot. if not, go to a different section of code for this guy. a no slot san switch would look like "pnj-sansw110-58" where 58 is obviousy just the port.

i tried something like "pnj-sansw124-`([0-9]\{1\}|[0-9]\{2\})`//" but that didn't work out right.

thoughts?

-pupp

You need to use egrep or grep -E to have extended regular expression support.

i tried egrep but not working for me.

this is ksh btw. sorry if it wasn't posted earlier.

egrep pnj-sansw124-'([0-9]\{1\}|[0-9]\{2\})' io1 | while read jerk
do
        echo $jerk
done

basically getting nothing at this point.

Well, while testing this I think I'm running into a grep bug on HP-UX... it's working fine for me on Linux. What operating system are you using exactly?

Anyway, this *should* work:

echo "pnj_sansw124_123" | grep -w  'pnj_sansw124_[0-9]\{1,2\}'

However it does not work on HP-UX... as soon as you add the -w it fails to match, I can't for the life of me figure out why.

If you do choose to use egrep, or grep -E, it uses the Extended Regular Expression syntax, which has a subtle difference in the repetition syntax, no backslashes required before the squiggly brackets, e.g.:

echo "pnj_sansw124_12" | grep -Ew 'pnj_sansw124_[0-9]{1,2}'

running on sol10.

i ended up not using the "short cut" way with

egrep pnj-sansw124-'([0-9]\{1\}|[0-9]\{2\})'

so i changed it to

egrep pnj-sansw124-([0-9]|[0-9][0-9])/

this seems to work. however, obviously we start running into problems with higher numbers or more then two digit numbers. i don't feel like running all those ranges out. but for purposes for what i need it for now, its good. i'll fine tone it later with your suggestion.

now with grep -E, i fail. it didn't like that. egrep is good though.

thanks for the help. let me get to work and give it a go.

-pupp

Well, I've tested it on my Solaris 10 box and grep appears to behave as expected there.

If you choose to use egrep, the only caveat is that the default /usr/bin/egrep doesn't support the {m,n} repetition syntax (why, oh why don't Sun ever update these utilities!?!) so you may prefer to use the less prehistoric version in /usr/xpg4/bin/egrep. Also, egrep does not support -w, so you need to surround your expression with \<...\> to match words.