Using grep

Introduction

Grep stands for Globally search a Regular Expression and Print. The basic usage of the command is grep [options] expression filename. Grep will by default display any lines in a file that contain the expression.

The Options

The main options we will use in this tutorial are -P, -v, -i, -c, --color, -L, and -l. The options will be covered in each of the following sections.

The -P option

There are four types of regex supported by grep. These are extended, fixed, basic and Perl. The -P option enables Perl Compartible Regular Expressions also known as PCRE. PCRE is the most commonly used regex and what our regex tutorial covers. In this tutorial, we will cover basic and PRCRE. If you are doing anything using PCRE then use grep -P "expression" filename.

Reversing a match using -v

When running a grep match on a file you will sometimes want to exclude lines. To do this use grep -v “expression” filename. For instance, if you want to view your Apache logs and exclude any googlebot lines you would use grep -v "googlebot" /var/log/httpd/access_log.

Case insensitive searches

The -i option allows you to do case insensitive searches. Using grep -i "SeArCh" filename will find any occurrence of the term “search” no matter what the case.

Count the number of lines that match

If you are not interested in the lines that match, only the number of lines that match, then -c is the option to use. For example to find out how many times your wp-cron file has been called then grep -c wp-cron /var/log/httpd/access_log will give you that figure.

Setting color options

If you want the matches to be highlighted you can turn on the –color option. On Ubuntu, it is on by default but on Centos is it off by default. To turn it on use grep --color=always "pattern" filename. To make this the default in Centos add this to your ~/.bashrc file:

alias grep='grep --color=auto'

Matching and non-matching files

The -l and -L options return the filenames only when there is either no match present or there is a match present. This is not much use when just searching within one file for a pattern. However, if you are using the find command, for example, and it finds multiple files which you then run grep on, you may want just the file name returned of matching or non matching files. The -L option returns only filenames of files that have no match for the pattern and -l returns only the filenames of files that do have a match for the pattern.

Basic usage and using PCRE

If you just want to search for a basic string then you can use grep with no options. For example, if you want to search for lines that contain a specific IP in a log file then you would use grep "10.0.0.1" filename. If on the the other hand, you want to use the PCRE expression to find a range of IPs then you would need to use grep -P "^10.0.0.1[0-9][0-9]" filename which would find any matches with an IP between 10.0.0.100 and 10.0.0.199.

Please see our tutorial on regex.

Conclusion

This covers the basic usage of grep, to find out more please see the man pages: man grep.

Updated on August 6, 2018

Was this article helpful?

Related Articles