 <p>You can use this awk using <strong>same regex as provided by OP</strong>:</p> <pre><code>awk -v re='[tsh]*est' '{ i=0; s=$0; while (p=match(s, re)) { p+=RLENGTH; i+=p-1; s=substr(s, p) } print i; }' file 14 15 35 </code></pre>