# awk pattern matching

**URL:** <https://community.unix.com/t/awk-pattern-matching/329949>\
**Category:** Shell Programming and Scripting\
**Created:** [June 1, 2013, 2:37pm UTC](https://community.unix.com/t/awk-pattern-matching/329949 "2013-06-01T14:37:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BeefStu](https://community.unix.com/letter_avatar/beefstu/32/6_449c533f586939b6e5f246632a46421f.png) [@BeefStu](https://community.unix.com/u/BeefStu)\
**Post date:** [June 1, 2013, 2:37pm UTC](https://community.unix.com/t/awk-pattern-matching/329949/1 "2013-06-01T14:37:40Z")

</div>

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

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

```

Thanks to all who answer

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/6_449c533f586939b6e5f246632a46421f.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [June 1, 2013, 2:50pm UTC](https://community.unix.com/t/awk-pattern-matching/329949/2 "2013-06-01T14:50:48Z")

</div>

Please use code tags, not icode.

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

Regards,  
Alister

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [June 1, 2013, 3:33pm UTC](https://community.unix.com/t/awk-pattern-matching/329949/3 "2013-06-01T15:33:59Z")

</div>

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

```

Match anywhere:

```nohighlight
(*ORA*|*ERROR*|*SP2*)

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [June 1, 2013, 3:38pm UTC](https://community.unix.com/t/awk-pattern-matching/329949/4 "2013-06-01T15:38:55Z")

</div>

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

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

```

with:

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

```

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

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

```

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