[root@localhost ~]# cat a.txt
abcdeabcde
[root@localhost ~]# cat 1.awk
function rindex(str,findstr,k,ns,nf)
{   ns=length(str)
    nf=length(findstr)
    for (k=ns-nf+1;k>=1;k--)
        if (substr(str, k, nf) == findstr)
            return k
    return 0
}
{print rindex($0,"cd")}
[root@localhost ~]# awk -f 1.awk a.txt
8