Try โ€‚โ€‰HackMD

Using regular expression in grep on mac by installing GNU version

When following this wonderful tutorial for grep on Youtube, we see that the BSD version of grep on mac does not support regular expression. The installation for the GNU version of grep in the tutorial is

brew install grep --with-default-names

However, we get an error message:

Error: invalid option: โ€“with-default-names

because the "โ€“with-default-names" has removed from homebrew. So, instead, we should use

brew install grep 

After installation, When we type

grep -V

in command line, the version might still not be GNU, but looks something like

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More โ†’

We need to paste the line

PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"

into the
usr/.bash_profile file
so that the computer sets the GNU version as default.

Some part of the usr/.bash_profile should look like

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More โ†’

Then we MUST reopen the terminal and check the version again

grep -V

and you should see something like this

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More โ†’

After successfully setting the grep to the GNU version, we can search for words using regular expression

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More โ†’

Reference

  1. Youtube tutorial
  2. add PATH
  3. All of Programming Textbook