RHCE Study Guide

This tutorial explains how to use grep command step by step with examples including how regular expressions are used with grep command for pattern search in multiple files.

Grep stands for global regular expression print. Grep searches supplied pattern in single file or multiple files. A pattern can be any text string such as a single character, multiple characters, single word, multiple words or a sentence.

Grep command syntax

Following syntax is used to search the pattern with grep command.

#grep [option] [regex] pattern path

In above command

grep :- This is the main command.

option :- Options are used to control search operation. Options define how pattern should be searched.

regex :- Regular expression are used to customize the search pattern.

pattern :- This is the text string which we want to search.

path :- This is the location where we want to search for the supplied pattern.

The option and regex are not compulsory but they are used to enhance the functionality of grep command. Before we understand them in details, let’s understand how grep command works.

How grep command works

Grep command searches specified pattern line by line in file. If it finds any match in any line, then it will print that line in terminal. This is the default behavior of grep command. For example following command will search for text “user” in file “user-data” and print all the lines which contain this text string.

#grep user user-data

Grep command Options

The options are used to control the default behavior. If options are not specified then default behavior will be used. If options are specified then grep will behave according to options. To understand it practically, let’s reverse above example. Now suppose we want to print all lines which do not contain text string “user” from file “user-data” then we will use following command.

#grep -v user user-data

The –v option is used to exclude the search pattern from result.

Following table lists some most frequently used grep command options

Option Description
-i To ignore the case. If this option is used, grep will search for specified pattern in both lower and upper case. Without this option grep will search for specified pattern only in specified case.
-v To search lines those do not contain the specified pattern.
-r To search recursively. If this option is used, grep will search in specified directory as we as all of its sub-directories.
-A To print additional lines after the matching string.
-B To print additional lines before the matching string.
-o To print only the matching contents.
-e To use multiple regex. To use more than one regex we have to use this option with each regex.
-E To use extended regex. If search pattern contains any meta characters, we have to use this option otherwise they will be treated as normal character.

Meta characters are caret (^), dollar ($), input redirect (<), output redirect (>), period (.), asterisk (*), question mark (?), square brackets ([]), pipe (|), curly brackets ({}), parentheses (()), plus (+) and backslash (\). Meta characters have special meaning in shell. Unless used with –E option, grep will treat them as regular characters. For example without –E option plus (+) is a regular character while with –E option it is a Meta character. Special use of Meta characters is explained in following section.

Grep command Regular expressions

Regular expressions are used to customize the search pattern. With regular expression we can instruct grep command in more customized way.

For example, by default grep will look for specified search pattern in entire line but with regular expression ^ we can force it to look only at the starting of lines.

#grep ^[search pattern] [search location]

The regular expression ^ is used to look only at the starting of lines.

Following table lists some most frequently used regex with grep command.

Regular Expression Description
^ (Caret) Match expression at the start of a line
$ (Question) Match expression at the end of a line
\ (Back Slash) Turn off the special meaning of the next character
[ ] (Brackets) Match any one of the enclosed characters
[-] Define a range to match
[^ ] match any one character except those enclosed in [ ]
. (Period) match a single character of any value, except end of line
* (Asterisk) match zero or more of the preceding character or expression
\{x,y\} match x to y occurrences of the preceding
\{x\} match exactly x occurrences of the preceding
\{x,\} match x or more occurrences of the preceding
‘[Ff]oo’ Match Foo or foo
‘T[eE][sS]t’ Match Test, TeSt, TEst and TESt
‘[0-9][0-9]’ Match any two digits numeric value
‘[a-zA-Z]’ Match any line which contains at least one letter
‘[^a-zA-Z0-9]’ Match everything which is not a letter or number
‘[0-9]\{3\}’ Match any number between 000 to 999
‘^\.’ Match any line which starts with a dot (.)
‘"sanjay"‘ Match word “sanjay” within double quotes
[:alnum:] Match all alpha numeric characters (a to z, A to Z and 0 to 9)
[:alpha:] Match all alphabets in any case (a to z and A to Z)
[:lower:] Match all alphabets in lower case (a to z)
[:upper:] Match all alphabets in upper case (a to z)
[:digit:] Match all numeric characters (0 to 9)
[:blank:] Match only blank characters (space and tab)
[:space:] Match all space characters (space, tab, new line, enter, carriage return, vertical tab and form feed)

LAB Setup for practice

For demonstration purpose I will setup a practice lab. If you are familiar with grep command, there is no need to setup this lab. Grep command does not change anything itself. It only prints the search result. You can safely use it with existing file system. If you are skipping following lab setup, make sure you do not change anything in existing file system or revel confidential information from system unless you know what you are doing.

In this lab we will create some files to understand how grep command searches the supplied pattern exactly. Create a directory named “rhcelab”. In this directory create a file named userdata with some dummy user records.

grep command test file

Create a directory scriptdir and move in it. Create a simple script file named userprofile from .bash_profile and .bashrc files. The .bash_profile and .bashrc both are script files which are used to control the user environment. Both files are available in user’s home directory.

grep command script file for test

Create a directory named “html” and move in it. Copy the /usr/share/doc/HTML/en-us/index.html file in it.

grep command test html file

Create another directory named “custom” and move in it. Create a file named “custom-file” with some dummy contents.

grep command test file custom

That’s all setup we need to practice.

Grep command search pattern (Finding a word in file)

To find a word in file, following command is used.

#grep word path

In this command word is the search pattern which we want to search and path is the name and exact location of file where we want to search the pattern. Let’s start with a simple example. Suppose we want to search the word Sanjay in userdata file then we will use following command.

#grep Sanjay userdata

Following figure illustrate the use of above command.

grep sanjay

Grep command practical example

Figure out whether group nfsuser and group sambauser exist in system or not

Group database is stored in /etc/group file. To figure out whether a particular group exist or not we will use following command

#grep [name of group] /etc/group

If group exists, it will be printed on terminal. If it does not exist, nothing will be printed on screen.

grep group

As we can see in above output, group nfsuser exists and group sambauser does not exist.

Grep command practical example

Figure out whether a particular exist in system or not

User database is stored in /etc/passwd file. To figure out whether a particular user exist or not we will use following command

#grep [name of user] /etc/passwd

Just like group, if user exists, it will be printed on terminal. If it does not exist, nothing will be printed on screen.

grep user

As above output shows user sanjay exist while user john does not exist.

Grep command search pattern (Performing case insensitive search)

By default grep matches case. It means for grep sanjay and Sanjay both are different words. To see it in action, let’s search word Sanjay again in userdata file. Make one more search, but this time use word “sanjay”. Now compare the result of both commands. You will see the different output for both commands. Finally search one more time, but this time use –i option with command. The –i option is used to perform a case insensitive grep search.

Following figure illustrate this example step by step.

grep ignore case search

As above output shows when we searched for word without –i option, it returned only the lines which contain exact match while when we searched with –i option, it returned all lines which contain that word without matching the case.

Grep command search pattern (Searching the lines which start with a specific word or pattern)

By default grep will search for specified search pattern in entire line. To look only at starting of line we can use ^ regular expression with search pattern. This regular expression is mostly used while working with configuration files or shell scripts. In a well written configuration or script file, comments provide detailed information about that file. In configuration or script file any line which starts with # sign is treated as comment line. While processing a script or configuration file, Linux ignores all lines which start with # sign. We can use ^ regular expression with grep command to view the comments only from any script file. Following figure illustrates comments only for the file user_profile which we created for testing.

grep pattern search

Grep command practical example

Print the comment only from the file /etc/yum.conf to figure out where we should put our custom repositories.

The /etc/yum.conf file stores configuration values of yum. To view only comments from this file, we can use following command.

grep yum conf

If we only want to view the configuration values stored in this file, we can revert above command with -v option.

grep regex search

Grep command search pattern (Searching the lines which end with a specific word or pattern)

To look only at the end of line, the $ regular expression is used with search pattern. To see it in action, let’s search users who work in sales department from userdata file.

grep regular expression search

Grep command practical example

Print the list of users who use bash shell

The /etc/passwd file stores user information. Each user has a unique entry in this file. Which shell would be available to user is stored in last column of that entry.

grep user shell

Have you noticed any difference between the use of ^ and $?

The ^ is used before the search pattern while $ is used after the search pattern.

Basically $ say there should be nothing in line after it. For this reason it is used in the end of the search pattern.

Grep command search pattern (Removing blank lines or empty lines from file)

Since regular expression $ says there should be nothing after it, we can use it to check an empty or blank line. If we use ^ [starting point] with $[ending point] as ^$ then it will says there should be nothing in that line from starting. Since there is nothing in line, it would be a blank line. For example to print all blank lines from file user_profile, we will use following command.

#grep ^$ user_profile

Once we know how to print all empty lines, we can easily invert that with -v option.

#grep -v ^$ user_profile

To understand it practically, move in scriptdir and print the content of file user_profile in terminal. Now run above commands to print it again in terminal with or without blank lines.

Following figure illustrates this practice.

grep remove blank lines
Tips

This command may not work if file is created on Windows system. To remove blank lines from file which is created on other systems except *nix, use ‘^[[:space:]]*$’ regular expression.

Grep command search pattern (Using multiple regular expression)

We can use single regular expression without any option. To use multiple regular expressions, we have to use -e option with each regular expression. We used ^$ regex (regular expression) to filter the blank line. We also used ^# to filter the comment line. Let’s combine both regex to filter the blank lines and comment lines in one go with –v option.

grep remove blank lines and comments line

Grep command search pattern (Searching a sentence or search pattern which contains white space)

To search a sentence or multiple words which contain white space, we have to enclose them with quotes. If we do not enclose the white space then grep will search only the first word from specified sentence. Remaining words would be treated as files where grep will search for the specified pattern. To see, how it works, let’s search user name Sanjay Gupta in userdata file without quote and with quote.

grep search a sentence

As we can see in above output, when we did not enclose the string Sanjay Gupta with quotes, only the word Sanjay was search and the word Gupta was taken as file path. But when we enclosed the string Sanjay Gupta with quotes, it was considered as a search string. So whenever we have search string that contains white space, we should always enclose it with quotes otherwise it will not work as excepted.

Grep command search pattern (Searching in output of other command)

Usually grep is used to search in files for search pattern. But it can also be used to search in the output of other command. In Linux shell, pipe sign is used to connect the two commands at command prompt. The output of first command (left side command) is redirected to second command (right side) instead of standard output device. So whatever is the output of first command is the input of second command. Once connected with pipe sign we can filter the output of any command with grep command.

Grep command practical example

Search a particular rpm is install in system or not

The rpm -qa command is used to list the installed package in system. We can redirect this output in grep command with pipe sign. Later grep command can be used to check the specific package in output of first command.

Following figure illustrates the search for vsftpd package in output of rqm –qa command.

rpm -qa vsftpd

Grep command search pattern (Searching in all files and directories recursively)

To search in all files and directories recursively, the -r option is used. Grep will start searching for specified pattern from directory which will be specified as argument. If specified directory contains another directory then it will search that directory and all of its subdirectories as well. In our example, we created a multilevel directory structure to understand how grep command works recursively. In that example, custom_file which is located in last directory contains some dummy text string. Let’s find that string from root directory.

grep search in multi level directory

Depending on sub-directories level and number of files, recursive search may take some time. If you want to search only in few files then it is better to specify them as argument in grep command. Grep allows us to specify multiple file names as argument. For example following command will search text string “test” in file1, file2, file3 and file4 only.

#grep test file1 file2 file3 file4

So far in this tutorial we have worked with basic regex. In last section of this tutorial we will understand extended regex.

Grep command extended regex (regular expression) examples

Search all links from html file with grep command

In our lab setup we created an html file. Let’s use that file to understand above example. To extract all links from html_file, we will use following command.

#grep –Eoi ‘<a[^>]+>.*</a>’ htm_file

Let’s understand above command in detail.

Used options

-E :- we used this option because our search pattern contains + Meta character. Here we are not searching for + sign. We are using it as a regex. Without –E option grep will treat it as a normal character (+ sign) instead of a regex Meta character.

o :- By default grep will print entire line which contains the search string. But here we are interested only in the search string. The –o option is used to print only the matching words instead of entire line.

i :- We used this option to ignore the case.

Used regex

<a :- Starting point of anchor tag.

[^ >] :- Match everything except >.

+ :- Match preceding character one time.

> :- Ending point of anchor tag.

So far this search string says, search for the text which starts with <a and contains anything after it except > sing (because this sign is used to end the tag and we need some values before end otherwise it will become <a > which is not a valid anchor tag) and ends with > sign.

.* Regex dot (.) represents any single character and star (*) represents any number of characters. We used both together to search for any characters between starting and closing anchor tag.

</a> :- This is the closing point of anchor tag.

Collectively above search patterns says search the text which

starts with <a

and contains some value

and ends with >

and again contains any value

and ends with </a>

In more simple language <a some value > any value </a>.

Following figure illustrates the use of above regex

grep all links from file

Now suppose we are only interested in anchor tags. In that case, we can strip the linked text with following command.

grep extended regex

Let’s do more practice with this example

Extract only href=“” attribute from tag and redirect the output to next grep command which extract only URL of links. Finally store all links in a file.

Following figure illustrates this process

grep extened regex example

This way with grep command and regular expression we can extract any information from text file. Following table lists some more examples of grep command.

Grep Command Examples

Example Description
#grep ‘sanjay’ userdata Search file “userdata” for word “sanjay” anywhere in line.
#grep –n ‘sanjay’ userdata Display line number in search result.
#grep –i ‘sanjay’ * Search all files from current working directory for word “sanjay” in any case and anywhere in line.
#grep -w ‘sanjay’ userdata Search exact word “sanjay” in file “userdata”.
#rpm -qa | grep httpd Search for word httpd in the output of rpm –qa command.
#grep ‘^test’ conf-file Search word test only at the start of line in conf-file
#grep ‘test$’ conf-file Search word test only at the end of line in conf-file
#grep ‘sanjay[-]goswami’ userdata Search word “sanjay goswami” or “sanjay-goswami” in userdata file
#grep ‘\^conf’ vsftpd.conf Search all lines which start with ^conf in vsftpd.conf file
#grep ‘^conf’ vsftpd.conf Search all lines which start with conf in vsftpd.conf file
#grep ‘^From:‘ /usr/mail/$USER Search all mails of current user
#grep –r -l ‘ip address’ /etc/ Print all filenames which contain text string “ip address”
#grep –B3 ‘web root’ httpd.conf Display all matches along with three lines before the every match from file httpd.conf
#grep –A3 ‘web root’ httpd.conf Display all matches along with three lines after the every match from file httpd.conf
#grep –B3 –BA ‘web root’ httpd.conf Display all matches along with three lines before and after the every match from file httpd.conf
#grep –v –e ‘^$’ linux-file Print everything except blank lines from linux-file. Work only with files which are created in *nix system.
#grep -v –e ‘^[[:space:]]*$’ windows-file.txt Print everything except blank line from windows-file.txt. Work with files which are created in any system.

That’s all for this tutorial. In next tutorial we will learn another Linux topic in detail with examples.