Re: Does awk have a 'like' operator? (1907 Views)
Reply
Advisor
Jamie Collins
Posts: 28
Registered: ‎10-12-2004
Message 1 of 4 (1,907 Views)
Accepted Solution

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?
Honored Contributor
Sridhar Bhaskarla
Posts: 6,350
Registered: ‎08-15-2001
Message 2 of 4 (1,907 Views)

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
Bob Smith_23
Posts: 18
Registered: ‎07-01-2001
Message 3 of 4 (1,907 Views)

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
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 4 of 4 (1,907 Views)

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!
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.