Monday, April 4, 2022

[SOLVED] How to find one of several strings via grep?

Issue

I want to find files which contains one of the several strings. Here is example:

grep -e "max|min" <<< "emaxxmin" #it prints nothing
grep -e "min" <<< "emaxxmin" #it works
grep -e "max" <<< "emaxxmin" #it works

I confuses me a little bit. In my opinion regular expression is correct (it uses "choice" operator). man command also mentions about special treatment of | symbol


Solution

Like this:

grep -E '(max|min)' <<< "emaxxmin"

Or, with basic posix regular expressions:

grep '\(max\|min\)' <<< "emaxxmin"

Or, with the use of the -e argument (probably the simplest solution here?):

grep -e max -e min <<< "emaxxmin"


Answered By - hek2mgl
Answer Checked By - Candace Johnson (WPSolving Volunteer)