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
pamu
2
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
rdrtx1
3
try also:
line="Some text with ERR message"
index=$(echo "${line%%ERR*}"| wc -c)
echo $index
binlib
4
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}