grep は検索パターンに一致する単語のみを表示できますか? 質問する

grep は検索パターンに一致する単語のみを表示できますか? 質問する

検索式に一致するファイルから grep 出力の「単語」を作成する方法はありますか?

たとえば、複数のファイル内で「th」のすべてのインスタンスを検索したい場合は、次のようにします。

grep "th" *

ただし、出力は次のようになります (太字は私によるものです)。

some-text-file :猫はマットの  上に座った
その他のテキストファイル:クイックブラウンフォックス  yet-another-text-file :これで十分に説明できた
と思います 

同じ検索を使用して出力したいのは次のようになります。

the
the
the
this
thoroughly

これは grep を使用して可能ですか? または、別のツールの組み合わせを使用して可能ですか?

ベストアンサー1

試すgrep -o

grep -oh "\w*th\w*" *

編集: Phil のコメントと一致しています。

からドキュメント:

-h, --no-filename
    Suppress the prefixing of file names on output. This is the default
    when there is only  one  file  (or only standard input) to search.
-o, --only-matching
    Print  only  the matched (non-empty) parts of a matching line,
    with each such part on a separate output line.

おすすめ記事