3
grep -w uses punctuations and whitespaces as delimiters. 

単語の区切り文字として空白のみを使用するように grep を設定するにはどうすればよいですか?

4

4 に答える 4

3

スペースだけを一致させたい場合:はとgrep -w foo同じgrep " foo "です。行末やタブも一致させたい場合はgrep '\(^\| \)foo\($\| \)'、次のようなことを始めることができます。perl -ne 'print if /\sfoo\s/'

于 2012-06-07T12:27:29.610 に答える
2

働き方を変えることはできませんgrep -w。ただし、句読点を、またはを使用Xして文字に置き換えてから、を使用すると、うまくいきます。trsedgrep -w

于 2012-06-07T12:10:16.287 に答える
2

--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、検索パターンをより詳細に制御したりすることもできます。

于 2012-06-07T12:16:18.063 に答える