{ pad = s = match($0, /[A-Z]/); pad = RSTART clone = substr($0, RSTART + 1) while (match(clone, /[A-Z]/)) { clone = substr(clone, RSTART + 1) pad += RSTART } print $0, ": ", s, length - --pad } Save in a file (Eg.: find_index.awk) with #!/bin/awk -f as the first line (shebang) and run as ./find_index.awk yourfile
If perl is fine :
perl -lne ' /[A-Z]/; $s = $+[0]; reverse =~ //; print "$_ : $s - $+[0]" ' sample