grep -w只有空格作为分隔符

grep -w uses punctuations and whitespaces as delimiters. 

我怎样才能设置grep只使用whitespaces作为一个单词的分隔符?

如果你只想匹配空格: grep -w foogrep " foo "是一样的。 如果你还想匹配行结束符或制表符,你可以开始做这样的事情: grep '\(^\| \)foo\($\| \)' ,但是你可能最好用perl -ne 'print if /\sfoo\s/'

你不能改变grep -w工作方式。 不过,你可以使用trsed来替换标点符号,比如说用X来代替,然后使用grep -w来实现。

–word-regexp标志是有用的,但有限。 grep手册页说:

  -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编写一个扩展的正grep --extended-regexp ,让你更好地控制你的搜索模式。