Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Does awk have a 'like' operator?

SOLVED
Go to Solution
Highlighted

Does awk have a 'like' operator?

Wondering if there's a way to use a like operator in an if statement within awk..

awk '{if ($1 'like' "Joh%".....

Do I use an * wildcard or what?
3 REPLIES
Honored Contributor Honored Contributor

Re: Does awk have a 'like' operator?

Hi Jamie,

"~" is like like operator.

awk '$1 ~ /typ/ {print $1}' data

will print all $1's that has the string 'typ' in them.


-Sri

You may be disappointed if you fail, but you are doomed if you don't try
Advisor

Re: Does awk have a 'like' operator?

also, you can use !~ to find lines that are 'not like' the string you are searching for.
Honored Contributor Honored Contributor

Re: Does awk have a 'like' operator?

We can do with awk's default function index too as,


awk '{ if ( index ($1,"Joh%") == 1 ) print $1 }'

Upon getting index on inputstring the Joh% will make to print $1 string there.

HTH.
Easy to suggest when don't know about the problem!