problem with awk command

I have written some code in a KSH script to find the index of the character in a string like this

#!usr/bin/ksh

string="Hi How are you"
awk 'BEGIN {print index ($string, "are")}'

My expected output should be : 8
but it is giving the out put as : 0

I wanted to store that index value in a variable. Can anyone please help me in this

Also I am trying to get a substring of the above string by providing the start index and end index and store the substring in another variable. I am not getting any clue how to do that.

Guyz please help

Hi.

In your code, string is a shell variable, not an awk one:

$ awk -v s="$string" 'BEGIN {print index (s, "are")}'
8

(usr nawk or /usr/xpg4/bin/awk on Solaris)

I am not getting the expected output :frowning:

I wrote the code like this

#!/usr/bin/ksh
echo "-------START-----"
string="Hi How are you"
awk -v s="$string" 'BEGIN {print index (s, "are")}'
echo "-------END-------"

The output is:

-------START-----
awk: syntax error near line 1
awk: bailing out near line 1
-------END-------

Hi.

As I said above:

The old, cruddy awk on Solaris doesn't understand much, -v being one thing.

I am not getting you. do you want me to try this way?

#!/usr/xpg4/bin/awk
echo "-------START-----"
string="Hi How are you"
awk -v s="$string" 'BEGIN {print index (s, "are")}'
echo "-------END-------"

Hi.

No, I meant:

#!/usr/bin/ksh
echo "-------START-----"
string="Hi How are you"
nawk -v s="$string" 'BEGIN {print index (s, "are")}'
echo "-------END-------"

Or

#!/usr/bin/ksh
echo "-------START-----"
string="Hi How are you"
/usr/xpg4/bin/awk -v s="$string" 'BEGIN {print index (s, "are")}'
echo "-------END-------"
string="Hi How are you"
expr index "$string" "are"

Hi thanks, it is working. But I want to store this in a variable. please help in doing this

res=$(nawk -v s="$string" 'BEGIN {print index (s, "are")}')