Tuesday 21 April 2020

An `egrep` example with multiple regular expressions

Linux Tutorial and Material, Linux Guides, Linux Certifications, Linux egrep, LPI Exam Prep

Summary: How to use the Linux egrep command with multiple regular expressions (regex patterns).

As a quick note here today, I just used the Linux egrep command to perform a case-insensitive search on multiple regular expressions (regex patterns). Really, what I did was a little more complicated:

locate -i calendar | grep Users | egrep -vi 'twiki|gif|shtml|drupal-7|java|PNG'

As you can see from that command, I did this:

◉ Used to locate command with the case-insensitive option to find all files with the string "calendar" in them.

◉ Used the grep command so the output would only display files and directories with the string "Users" in them.

◉ Used the egrep command with multiple regex patterns to reduce the output much more. I used the -v argument to perform the "opposite" meaning of a normal egrep command, so strings with these patterns were not shown; and also used the -i argument to perform a case insensitive egrep search here.

While my original locate -i calendar command shows nearly 3,000 files, the locate command combined with grep and egrep in this manner shows only 15 files.

An easier egrep command


Before I go away, here's an easier egrep command to look at:

egrep 'apple|banana|orange' *

That egrep command searches for those three strings (regular expressions, really) in all files in the current directory. This next command does the same thing, but in a case-insensitive manner:

egrep -i 'apple|banana|orange' *

Related Posts

0 comments:

Post a Comment