grep -w 的作用是什么?

grep -w 的作用是什么?

这里grep 与选项一起使用-w。我做了man grepgrep --help试图找到上述选项的作用。两个输出都没有说明任何有关-w选项的信息。

该选项有什么作用?为什么它没有出现在manor中--help?如果再次发生类似的情况,我还能在哪里查看答案?

我目前正在使用 Ubuntu,如果相关的话(是吗?)

答案1

# grep --help | grep -e -w
  -w, --word-regexp         force PATTERN to match only whole words
  -H, --with-filename       print file name with output lines
  -L, --files-without-match  print only names of FILEs with no selected lines
  -l, --files-with-matches  print only names of FILEs with selected lines
# grep PRETTY /etc/os-release
PRETTY_NAME="Ubuntu 18.04 LTS"
# man grep | grep -e -w -A1
       -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.

答案2

-w, --word-regexp 仅选择包含构成整个单词的匹配项的行。测试是匹配的子字符串必须位于行的开头,或者前面有一个非单词组成字符。同样,它必须位于行尾或后跟非单词组成字符。单词组成字符是字母、数字和下划线。

来源:https://linux.die.net/man/1/grep

相关内容