Getting the index of a string in a string

I have a variable

$line= Some text with ERR message

I want to get the value of the index of ERR and assign to a variable say $index

I expect the output

echo $index

16

I tried to use match function of awk, but unable to pass a string as input instead of a file

Also tried the following code

index=awk '{for(i=1;i<=NF;i++) if($1~/^ERR/){print i}'<$line

Use sth like this..

line="Some text with ERR message"
index=$(echo $line | awk '{for(i=1;i<=NF;i++) if($i~/^ERR/){print i}}')
echo $index
1 Like

try also:

line="Some text with ERR message"
index=$(echo "${line%%ERR*}"| wc -c)
echo $index
expr 'Some text with ERR message' : '.*ERR' - 2
16
awk 'BEGIN{print index("Some text with ERR message", "ERR")}'
16

Shell:

line="Some text with ERR message"
first=${line%%ERR*}x
index=${#first}