What does grep -w do?

7,761

Solution 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.

Solution 2

-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.

Source: https://linux.die.net/man/1/grep

Share:
7,761

Related videos on Youtube

CcVHKakalLLOOPPOkKkkKk
Author by

CcVHKakalLLOOPPOkKkkKk

Updated on September 18, 2022

Comments

  • CcVHKakalLLOOPPOkKkkKk
    CcVHKakalLLOOPPOkKkkKk over 1 year

    In here grep is used with the option -w. I did man grep and grep --help to try to find what the aforementioned option does. Neither output says anything about a -w option.

    What does that option do? Why does it not appear in manor --help? In case something similar happens again, where else can I check for an answer?

    I am currently using Ubuntu, if that is relevant (is it?)

    • Michael Homer
      Michael Homer almost 5 years
      My man page and --help (from Debian, probably older than yours) both list -w, --word-regexp force PATTERN to match only whole words. Which grep version are you using?
    • Jeff Schaller
      Jeff Schaller almost 5 years
      And/or Ubuntu version...?
    • Stéphane Chazelas
      Stéphane Chazelas almost 5 years
      If on a GNU system, try info -- grep -w instead.