Grep пропускает совпадения в абзаце с несколькими вхождениями шаблона

Добавьте это в~/.inputrc

set show-all-if-ambiguous on

Цитата из arch wiki по этой теме

Or you can set it such that a single tab will perform both steps: partially complete the word and show all possible completions if it is still ambiguous:

1
02.10.2019, 13:52
1 ответ

Проблема в том, что вам нужно выполнять поиск итеративно, каждый раз удаляя первую совпавшую часть:

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
0
28.01.2020, 00:00

Теги

Похожие вопросы