Hi guyz i want to know nth position of character in string. For ex.
var="UK,TK,HK,IND,AUS"
now if we see 1st occurance of , is at 3 position, 2nd at 6,..4th at 13 position.
1st position we can find through INDEX, but what about 2nd,3rd and 4th or may be upto nth position. ?
In oracle we had instr there we could provide the nth number occurance also. Here how we can achieve it ?
FYI i am beginner in unix and i m using KSH
Is this a homework assignment?
What operating system and version of ksh are you using? If you don't know, show us the output from the commands:
uname -a
print ${.sh.version}
what /bin/ksh | grep Version
You could try this function:
function index
{
p=1
f=0
t="$1"
catch=""
while [ $p -le ${#1} ]
do
if [ "${t#$2}" != "$t" ]
then
let f=f+1
if [ $f -eq ${3:-1} ]
then
echo $p
return
fi
fi
t="${1#$catch?}"
catch="${1%$t}"
let p=p+1
done
echo 0
}
Call it like this:
index "UK,TK,HK,IND,AUS" , 4
# Result: 13
Or even this:
index "UK,TK,HK,IND,AUS" "?K" 3
# Result: 7