grep -w uses punctuations and whitespaces as delimiters.
単語の区切り文字として空白のみを使用するように grep を設定するにはどうすればよいですか?
スペースだけを一致させたい場合:はとgrep -w foo
同じgrep " foo "
です。行末やタブも一致させたい場合はgrep '\(^\| \)foo\($\| \)'
、次のようなことを始めることができます。perl -ne 'print if /\sfoo\s/'
働き方を変えることはできませんgrep -w
。ただし、句読点を、またはを使用X
して文字に置き換えてから、を使用すると、うまくいきます。tr
sed
grep -w
--word-regexp フラグは便利ですが、制限があります。grep の man ページには次のように書かれています。
-w, --word-regexp
Select only those lines containing matches that form whole
words. The test is that the matching substring must either be
at the beginning of the line, or preceded by a non-word
constituent character. Similarly, it must be either at the end
of the line or followed by a non-word constituent character.
Word-constituent characters are letters, digits, and the
underscore.
カスタム フィールド セパレータを使用する場合は、awk の方が適している場合があります。または、egrep を使用して拡張正規表現を記述したりgrep --extended-regexp
、検索パターンをより詳細に制御したりすることもできます。