Проблема в том, что вам нужно выполнять поиск итеративно, каждый раз удаляя первую совпавшую часть:
string="word1 word2 word3 word4 the word5 word6 word7 word8 word9 the quoi écrire hihi haha the a b c d e"
copy=$string
while m=$(grep -Eo '([^ ]+ ){4}the( [^ ]+){5}' <<<"$copy"); do
echo "$m" | head -1 # print just the first one
copy=${copy#* the } # remove up to and including the _first_ " the "
done
word1 word2 word3 word4 the word5 word6 word7 word8 word9
word6 word7 word8 word9 the quoi écrire hihi haha the
quoi écrire hihi haha the a b c d e
Или используйте встроенную в bash поддержку регулярных выражений, что означает, что вам не нужно анализировать какие-либо grep
выходные данные для вывода первого совпадения:
copy=$string
# the pattern is *unquoted*
while [[ $copy =~ ([^ ]+ ){4}the( [^ ]+){5} ]]; do
echo "${BASH_REMATCH[0]}"
copy=${copy#* the }
done