What to do if grep: empty (sub) expression appears on Mac grep

--In many cases, you use a Mac to grep for work or hobbies to search or aggregate. --At that time, you may encounter the error as the title. ――Therefore, this time, I will record two points to be taken when this error occurs.

Conclusion

--There are the following two methods. ** * If you are in a hurry, we recommend installing the GNU version. ** ** --Introduce the GNU version of grep with brew install grep. -** * Mac standard grep is BSD version. ** ** --Modify the way to write grep so that it works on BSD.

-** * Detailed contents are explained in the following Contents orchids. ** **

environment

Contents

Error source

--The error occurs in the following example.

#Search for Hello or World or empty in the string.
echo -e "Hello\nWorld" | grep -E '(Hello|World|)'

#Error output
grep: empty (sub)expression

Cause

--The following are possible causes. -** A regular expression search adds an empty space to the candidates. ** ** -** I've heard that the GNU and BSD versions have different detailed behaviors. ** ** --For verification, when I verified it in the GNU grep environment prepared by Docker, there was no problem with the above writing method. ――From this, it was found that the cause was a small difference in behavior in the processing when empty was selected.

Elimination

--To identify the cause, solve it by the following two methods. --Install the GNU version of grep. --Modify to write grep so that it works on BSD --The first GNU version is completed by simply passing the following command. - brew install grep ――The second is to change the processing when empty is selected as follows.

#Recognize with symbols at the beginning and end of the line.
echo -e "Hello\World" | grep -E '(^|Hello|World)'

#Deleted the empty selection process itself.
echo -e "Hello\nWorld" | grep -E '(Hello|World)'

-** * There are advantages and disadvantages to both methods, so select the safe one **

Summary

--From the above, we recognize the small differences in behavior depending on each OS / library. ――Therefore, be aware of highly portable descriptions.

reference

Recommended Posts

What to do if grep: empty (sub) expression appears on Mac grep
What to do if `pip install matplotlib` fails on Mac
What to do if SciPy installation fails on CentOS
What to do if scrapy doesn't work after installing scrapy with pip on mac
What to do if pip install mysqlclient fails on MacOS
What to do if yum breaks
What to do if Python doesn't work on Git for Windows
What to do if Insecure Platform Warning appears when running Python
What to do if the inode is exhausted on EC2 Linux
What to do if PyAudio cannot be installed on Python 3.7, 3.8, 3.9 on Windows
What to do if you forget your login password on Manjaro Linux
What to do if you get stuck during Anaconda installation on Linux
What to do if No Python documentation found for ... appears in pydoc
What to do if pipreqs results in UnicodeDecodeError
What to do if you can't pipenv shell
Note: What to do if pip install fails
What to do if mod_fcgid cannot resolve UnicodeEncodeError
What to do if rails s doesn't work
What to do if pip cannot be installed
What to do if atom autocomplete-python doesn't work
What to do if Docker-sync suddenly stops working
What to do if "amazon-linux-extras" → "No module named amazon_linux_extras"
What to do if CERTIFICATE_VERIFY_FAILED occurs when nltk.download () is done on macOS pyhon
What to do if NotADirectoryError: [Errno 20] Not a directory:'xdg-settings' appears in jupyter notebook
What to do if Japanese language support is not completely installed on Ubuntu 16.04
What to do if an SSL connection error (ssl.SSLError: [SSL: DH_KEY_TOO_SMALL]) occurs on Ubuntu 20.04
What to do if (base) is displayed at the beginning of the Mac terminal
What to do if you installed pyenv on Mac OS using Homebrew but the python version doesn't switch
Notes on what to do if "macOS 11 or later required!" Appears in Big Sur or pyarrow2.0.0 cannot be installed
What to do if pip install fails in Xcode 5.1
[Go 1.13] What to do when unexpected directory layout: appears
[openpyxl] What to do when IllegalCharacterError appears in pandas.DataFrame.to_excel
What to do if "export" keeps appearing on terminal when trying to put Python on macOS
No module named What to do if you get'libs.resources'
What to do if the Microsoft Store opens even if you run python on Windows
ModuleNotFoundError: No module What to do if you get'tensorflow.contrib'
What to do if ENOENT: no such file or directory appears during npm install
What to do if you get an error when importing matplotlib in Python (Mac)
What to do if Python IntelliSense is not displayed in VS Code on Windows
What to do if a UnicodeDecodeError occurs in pip
What to do if pip install fails to install dependent libraries
What to do if sys / cdefs.h does not exist
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
What to do if pyenv is not enabled (zsh)
What to do if Jupyter Notebook on WSL does not start automatically in your browser
[* CentOS 6.10] What to do if you cannot add the IUS Community Project repository on CentOS 6.5 (VirtualBox)
What to do if you get an Undefined error:'Module_six_moves_urllib_parse' object has no attribute'urlencode' on MacOS
What to do if ʻarguments [0] .scrollIntoView ();` fails in python selenium
What to do when Japanese is not displayed on matplotlib
What to do if the package dependency cannot be repaired
What to do if you are addicted to Windows character code
What to do if pip gives a DistributionError in Homebrew
What to do if you get "coverage unknown" in Coveralls
What to do if you can't sort files with subscripts
What to do if a 0xC0000005 error occurs in tf.train.start_queue_runners ()
What to do if package installation fails when deploying to heroku
What to do when "SSL: CERTIFICATE_VERIFY_FAILED _ssl.c: 1056" appears in Python
OSError: [Errno 40] What to do when Message too long appears
What to do when "Invalid HTTP_HOST header" appears in Django
What to do if you can't log in as root
What to do if Linux VLC can no longer rotate