grep は、次の特定の数の単語と一致します。

grep は、次の特定の数の単語と一致します。

キーワードと次の4つの単語をどのように検索しますか?たとえば、次の段落があるとします。

Meta Stack Exchange is where users like you discuss bugs, features, and 
support issues that affect the software powering all 167 Stack Exchange 
communities.

「Exchange」というキーワードと次の4つの単語を検索しようとするため、「Exchangeはユーザーが好きな場所です」という結果が出力されます。

私は以下を使用しました:

grep -Eo "Exchange" 

キーワード(単語、数字、チャート...)の後のgrep数を制御するには、このコマンドに追加する必要がありました。

ベストアンサー1

スペースとスペース以外の文字シーケンスを使用することもできますか?

$ grep -Eo 'Exchange([[:space:]]+[^[:space:]]+){4}' << EOF
Meta Stack Exchange is where users like you discuss bugs, features, and 
support issues that affect the software powering all 167 Stack Exchange 
communities.
EOF
Exchange is where users like

または(perlスタイル、grepがサポートしている場合)

$ grep -Eo 'Exchange(\s+\S+){4}' << EOF
Meta Stack Exchange is where users like you discuss bugs, features, and 
support issues that affect the software powering all 167 Stack Exchange 
communities.
EOF
Exchange is where users like

気づく grep 複数行にわたって一致しない - 複数行の一致の場合は、次のものを使用できます。 pcregrep 代わりに

おすすめ記事