Short Tip: egrep – using grep with more than one expression

I stumbled across an old blog post of mine about using grep with more than one expression: in the old days I used -e several times, one for each new expression. But as stressed in the comments that way is neither convenient nor reliable on ll platforms. And I have developed as well, so today I usually use egrep if I need to grep for several expressions. Thus, here are some short notes about using it.

The multiple arguments you are searching for a passed to egrep separated by pipes. For example, if you want to grep the output of lspci for all audio and video controllers, the correct command is:

$ lspci|egrep -i 'audio|vga'
00:05.0 Audio device: NVIDIA Corporation MCP61 High Definition Audio (rev a2)
00:0d.0 VGA compatible controller: NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2)

( Yes, I know, I write my blog post on pretty old hardware right now 😉 )

egrep does understand more than two expressions, so you can use the option like $STRING_1|$STRING_2|$STRING_3|.... But don’t forget to include the high tics ' in the command: these ensure that the pipe is used as a separator instead of being interpreted by your shell.

12 thoughts on “Short Tip: egrep – using grep with more than one expression”

  1. If you actually have one of those, then you are quite lucky it hadn’t exploded or melted yet (message from a tx1000-using user)

  2. I’m pretty sure I’ve missed out on extended regex several times, but my man page says, “Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified.” You might want the capital e switch with grep instead.

    1. Oh well, I never stop learning – however, I guess the alias is the best thing since adding options is waht I want to avoid…
      Thanks for pointing it out though. I guess I’ll have to write another short tip in some weeks 😉

  3. egrep is just ‘grep -E’

    I’ll one-up you on that, and suggest ‘grep -P’ which gives you even more useful regex features…

    1. Nice hint – however, I often work on various systems of various customers and have to work with what I find there. Thus ack is only an option for machines which are fully under my control…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: