`grep' 之间的区别' coco.txt` 和 `grep 'hi' coco.txt`

`grep' 之间的区别' coco.txt` 和 `grep 'hi' coco.txt`

grep '\<hi\>' coco.txt和之间有什么区别grep 'hi' coco.txt?我在终端中应用了这些命令,但没有看到任何区别。

答案1

在 中grep\<代表单词的开头\>代表单词结尾

man grep

The Backslash Character and Special Expressions
   The symbols \< and \>  respectively  match  the  empty  string  at  the
   beginning and end of a word.  The symbol \b matches the empty string at
   the edge of a word, and \B matches the empty string provided  it's  not
   at the edge of a word.  The symbol \w is a synonym for [_[:alnum:]] and
   \W is a synonym for [^_[:alnum:]].

因此,grep '\<hi\>'将匹配包含单词的任何行higrep 'hi'并将匹配包含字符序列的任何行hi

$ grep '\<hi\>' <<< "hi"
hi
$ grep '\<hi\>' <<< "chimes"
$ # no output since no match
$ grep '\<hi\>' <<< "hi-fi"
hi-fi
$ grep '\<hi\>' <<< "high voltage"
$ # no output since no match

相关内容