Skip to content


How to use the OR operator in grep

Linux

For anyone familiar with the Linux or UNIX command line interface it’s quite likely that grep has crossed your path sometime. grep is a wonderful command line tool that helps you search for a word or words through files. It ships with almost all flavors of Linux by default. For those who aren’t familiar with grep here’s a quick primer.

# grep www /etc/httpd/conf/httpd.conf

#ServerName www.example.com:80
DocumentRoot “/var/www/html”
# e.g., www.apache.org (on) or 204.62.129.132 (off).
Alias /icons/ “/var/www/icons/”

In the example above we are using grep to search for the term www in the file /etc/httpd/conf/httpd.conf. 4 results were returned. It’s a pretty straightforward command. grep ships with a lot more functionality. Feel free to browse through the manpage for grep, which you can get to by running man grep.

Now we’ll look at a feature of grep that allows you to search through a document for two terms, using command only. Let’s modify the command shown above to search for www, and also for the term server.

# grep ‘www\|server’ /etc/httpd/conf/httpd.conf

# SetHandler server-status
# Allow remote server configuration reports, with the URL of
# http://servername/server-info (requires that mod_info.c be loaded).
#ServerName www.example.com:80
DocumentRoot “/var/www/html”
# e.g., www.apache.org (on) or 204.62.129.132 (off).
Alias /icons/ “/var/www/icons/”
# SetHandler server-info
# enable the proxy server:
# (”Full” adds the server version; “Block” removes all outgoing Via: headers)
# use only name-based virtual hosts so the server doesn’t need to worry about
# server name.

So as you can see the output was much larger in the second command (12 lines were returned). Note how we modified the first command and changed the search term to ‘www\|server’. Remember to use the forward slash ( \ ) before the pipe, or else you will not get the same result. Alternately you can also use the tool egrep to run a similar search without using the forward slash. Try this:

# egrep ‘www|server’ /etc/httpd/conf/httpd.conf

# SetHandler server-status
# Allow remote server configuration reports, with the URL of
# http://servername/server-info (requires that mod_info.c be loaded).
#ServerName www.example.com:80
DocumentRoot “/var/www/html”
# e.g., www.apache.org (on) or 204.62.129.132 (off).
Alias /icons/ “/var/www/icons/”
# SetHandler server-info
# enable the proxy server:
# (”Full” adds the server version; “Block” removes all outgoing Via: headers)
# use only name-based virtual hosts so the server doesn’t need to worry about
# server name.

You can also add another pipe and search for another term also. You command would look like # egrep ‘www|server|apache’ /etc/httpd/conf/httpd.conf.

Posted in Linux.

Get Simple Help tutorials just like this one in your email inbox every day - for free! Just enter your email address below:

 

You can always opt out of this email subscription at any time.

One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. a says

    ‘/’ = slash
    ‘\’ = backslash
    author = troll



Some HTML is OK

or, reply to this post via trackback.