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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern Matching.. 1

Status
Not open for further replies.

jestrada101

Technical User
Mar 28, 2003
332
How can I see if there is a pattern in a string?

example

variableA="ABCDEFG"
variableB="D"

I'd like to know how to see if a variableB or "D" exists in variableA.

Thanks for any guidance..
JE
 
JE,

You can do the following -

print $A | grep $B

The only way the statement will return anything is if $B is a part of the $A variable.


Steve
 
You can also use expr :

if expr "$variableA" : ".*variableB.*" >/dev/null
then
# variable B exists in variable A
else

fi


With ksh you can do :

if [[ "$variableA" = *${variableB}* ]]
then
# variable B exists in variable A
else

fi


'expr' use Regular Expression
Ksh test use globbing patterns


Jean Pierre.
 
Yet another method:
Code:
case $variableA in *${variableB}*)
  echo "$variableB is in $variableA";;
esac

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top