O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The index (str, sub) function

The index (str,sub) function is used to get the index (location) of the given substring (or character) in an input string. On a success, it returns the position where the substring started; otherwise, it returns 0. The first character of the input str string is at position 1. If the substring consists of two or more characters, all of those characters should match in the same order for a nonzero return value. This function is useful for checking proper input conditions. The following is an example of the index (str, sub) function:

$ vi index.awkBEGIN    {     str = "Awk is a powerful utility. Unix is awesome."       search = "power"       location = index(str, search) printf("Substring \"%s\" found at location : %d\n", search, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required