awk pattern matching

can somebody provide me with some ksh code that will return true if my the contents in my variable match anyone of these strings ORA|ERROR|SP2

variable="Error:ORA-01017: Invalid username/password; logon denied\nSP2-0640:Not connected"

I tried this and it does not seem to work for me

 
if [[ "$variable" = @(ORA|ERROR|SP2) ]]
then
   echo "Match"
else
   echo "No Match"
fi 
 

Thanks to all who answer

Please use code tags, not icode.

With regard to your problem, the case statement is a natural fit.

Regards,
Alister

case "$variable" in
(ORA|ERROR|SP2)
  echo "Match"
  ;;
(*)
  echo "No Match"
  ;;
esac

Match anywhere:

(*ORA*|*ERROR*|*SP2*)

If you are using a Korn shell newer than the 1988 version, replace:

if [[ "$variable" = @(ORA|ERROR|SP2) ]]

with:

if [[ "$variable" == *@(ORA|ERROR|SP2)* ]]

if you want to match ORA, ERROR, or SP2 anywhere in variable, or with:

if [[ "$variable" == @(ORA|ERROR|SP2)* ]]

if you want to match ORA, ERROR, or SP2 only if they appear at the start of variable.

1 Like