Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Regular expr sp char with Or logic in grep 5

Status
Not open for further replies.

ljsmith91

Programmer
May 28, 2003
305
0
0
US
I have a variable $myvar that might end in "_test" or might not. I want to test on it using grep to see if it has this value and only at the end of the variable.

This code works:

Code:
test=echo $myvar | grep '_test$'

I then test the $test var like so:

Code:
if [ -z "$test" ]
    then
       Alert_Type="Production"
     else
       Alert_Type="Test"
 fi

This all works fine... but when I add my OR logic into the grep, I cannot get this to work in order to check for variations of "_test" like "_Test" or "_TEST". Here is one of the many examples of what I have tried with no luck:

Code:
test=echo $myvar | grep '_test$|_Test$|_TEST$'
Can anyone help ? Thanks.


 
You could either (1) use grep's ignore-case switch, -i; or (2) escape the bars in your regex, | -> \|.
 
I have tried the folling tests and have had no luck:

myvar=this_is_a_test

echo $myvar | grep -i "_test$|_Test$|_TEST$"

or

echo $myvar | grep "_test$\|_Test$\|_TEST$"

also tried:

echo $myvar | grep "[_test]$\|[_Test]$\|[_TEST]$"

I am just not getting the expected results. Any ideas as to why ?

 
I meant:
(1) grep -i '_test$'
or
(2) grep '_test$\|_TEST$\|_Test$'
 
TonyGroves,

That is what I have tried.... and it doesn't work. Maybe I am missing something but we have it coded exactly the same.

 

I am using HP-UX OS and cannot get this to work. Thanks for your input. Anyone else have any ideas ?
 
If you don't know how to use grep (tip: man grep) you may choose another way:
case $myvar in
*_test|*_Test|*_TEST) Alert_Type="Test";;
*) Alert_Type="Production"
esac

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
what's up ljsmith91.

see if the following will work for you:

#!/bin/sh

myvar="_test$"

test=$(echo $myvar | grep -i '_test\$')

if [ -z "$test" ]
then
alert_type="Production"
else
alert_type="Test"
fi

echo "$alert_type"

i changed the value of $myvar to various cases and it still seems to work.
 
Hi ljsmith91

Have you tried egrep or grep -E (depending on the flavour of *nix) - please check the man pages.

eg: echo $myvar | egrep '_test$|_Test$|_TEST$'

I hope that helps.

Mike
 
TonyGroves' suggestion of grep -i definitely seems to be the way forward.

I've just tried echo xxx_TeSt | grep -i '_test$' on HP-UX and it worked (that is, it displayed the string xxx_TeSt). It works whatever case I use in the word test.

I would also concur with PHV's advice to man grep to see the options in your environment.

If necessary, some variants of grep may allow multiple uses of the -e option (as suggested by Mike042). Again, man grep to check. For example echo xxx_TesT | grep -e '_Test$' -e '_TesT$' works. But you have 16 cases to cover to allow for all upper/lower combinations in test. So follow the grep -i route if you can.
 
Another thought... try losing the quotes. I don't think they are needed (not in my environment anyway).

So try echo xxx_TeSt | grep -i _test$
 
You guys are great. Thanks for all of your help. Several of your suggestions made a difference. Great day to all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top