The Grep Command

   

Introduction

The grep utility allows a user to search through a file for a pattern, and returns customized information about where that pattern exists within the specified file. Format: grep -options <search term> <file to search> <additional files to search >

The Data File

For the purposes of this guide we'll be using the following data inside a file named 'process.txt'
 3125 ?        Ss     0:01 x-session-manager
 3178 ?        Ss     0:00 /usr/bin/ssh-agent /usr/bin/dbus-launch --exit-with-session x-session-manager
 3182 ?        S      0:00 /usr/bin/dbus-launch --exit-with-session x-session-manager
 3183 ?        Ss     0:00 /usr/bin/dbus-daemon --fork --print-pid 4 --print-address 6 --session
 3186 ?        S      0:03 /usr/lib/libgconf2-4/gconfd-2 5
 3195 ?        S      0:00 /usr/bin/gnome-keyring-daemon
 3206 ?        Sl     0:06 /usr/lib/control-center/gnome-settings-daemon 
 3216 ?        Ss     0:49 /usr/bin/metacity --sm-client-id=default0
 3243 ?        S      0:00 /usr/lib/nautilus-cd-burner/mapping-daemon
 3248 ?        Ss     0:32 gnome-panel --sm-client-id default1
 3250 ?        Ss     0:24 nautilus --no-default-window --sm-client-id default2
 3267 ?        Sl     0:00 /usr/lib/gnome-vfs-2.0/gnome-vfs-daemon
 3271 ?        S      1:35 /usr/lib/gnome-panel/wnck-applet --oaf-activate-iid=OAFIID:GNOME_Wncklet_Factory 
 3275 ?        Ss     0:00 gnome-volume-manager --sm-client-id default4
 3281 ?        Ss     0:00 vino-session --sm-client-id default5
 3283 ?        Ss     0:00 bluetooth-applet
 3285 ?        Ss     0:03 update-notifier
The data was obtained by executing a 'ps ax' command on one of the servers here at [acid].

Example

General usage:
#grep blue process.txt 

3283 ?        Ss     0:00 bluetooth-applet
Using 'grep blue process.txt' yields all of the lines in process.txt that contain the word/pattern 'blue'. In this case, there was only one.

Common Uses

Find a pattern within a set of files

It is very common to want to find some pattern or text within all of the files in a directory, or directory tree. Perhaps you know what the output of a web page is, but you're not sure which of the 40+ scripts generates that text.
#grep 'this is a test' *.pl

filetest.pl:    print FILE "this is a test";
This command allows grep to search all of the .pl files in the current directory for the pattern 'this is a test'. grep returns the name of the file in which the pattern exists. We also could have searched * and grep would have looked through all of the files in the directory.

Find the line number that a pattern occurs.

#grep -n gnome process.txt
6: 3195 ?        S      0:00 /usr/bin/gnome-keyring-daemon
7: 3206 ?        Sl     0:06 /usr/lib/control-center/gnome-settings-daemon --oaf-activate-iid=OAFIID:
10: 3248 ?        Ss     0:32 gnome-panel --sm-client-id default1
12: 3267 ?        Sl     0:00 /usr/lib/gnome-vfs-2.0/gnome-vfs-daemon
13: 3271 ?        S      1:35 /usr/lib/gnome-panel/wnck-applet --oaf-activate-iid=OAFIID:
14: 3275 ?        Ss     0:00 gnome-volume-manager --sm-client-id default4
Here the matched pattern, 'gnome', is output along with the line numbers that pattern can be found on. The line numbers are the first number on each line, followed by a colon.

Search for a pattern containing a quote. (Showing line numbers with -n)

Not many quote characters in the process.txt so we'll use some literature.
#grep -n "the cough's a mere nothing" poe.txt
				
80:"Enough," he said; "the cough's a mere nothing; it will not kill me. I shall not die of a cough."
Here we match a selection of text enclosed within double quotes.
#grep -n 'Enough,"' poe.txt
				
80:"Enough," he said; "the cough's a mere nothing; it will not kill me. I shall not die of a cough."
And here we match a selection of text enclosed in single quote characters. It just so happens to be the same line.
We need to enclose single quotes in double quotes, or visa versa in order to capture correctly.

Determine the number of times a pattern occurs within a file

Often, it is simply necessary to determine the number of times something occurs within a file, for example a specific email address or name. grep uses the -c option to count the number of times your specified pattern occurs.
# grep -c daemon process.txt
5
# grep -c process process.txt
0
# grep -c /usr process.txt
10
process.txt contained the word 'daemon' 5 times, 'process' 0 times, '/usr' 10 times.

Find all of the lines in a file in which your pattern does not occur.

We can use grep to find all the lines which do not contain the pattern specified. If, for example, we wanted to find all the lines in a file that do not contain the letter 'e', we can do the following.
 #grep -v /usr process.txt
			
 3125 ?        Ss     0:01 x-session-manager
 3248 ?        Ss     0:32 gnome-panel --sm-client-id default1
 3250 ?        Ss     0:24 nautilus --no-default-window --sm-client-id default2
 3275 ?        Ss     0:00 gnome-volume-manager --sm-client-id default4
 3281 ?        Ss     0:00 vino-session --sm-client-id default5
 3283 ?        Ss     0:00 bluetooth-applet
 3285 ?        Ss     0:03 update-notifier
-v causes grep to match the opposite of what we'd match without that option. Above are all the lines that do not contain '/usr'

Match patterns regardless of case.

grep is case sensitive. To match patterns regardless of case, use the -i option.
//First let's see what we get without the -i option for the following case:
#grep GnOmE process.txt

#


//We didn't get ANY matches.
//And now with -i
#grep -i GnOmE process.txt

 3195 ?        S      0:00 /usr/bin/gnome-keyring-daemon
 3206 ?        Sl     0:06 /usr/lib/control-center/gnome-settings-daemon --oaf-activate-iid=OAFII
 3248 ?        Ss     0:32 gnome-panel --sm-client-id default1
 3267 ?        Sl     0:00 /usr/lib/gnome-vfs-2.0/gnome-vfs-daemon
 3271 ?        S      1:35 /usr/lib/gnome-panel/wnck-applet --oaf-activate-iid=OAFII
 3275 ?        Ss     0:00 gnome-volume-manager --sm-client-id default4
As you can see, this option allows a user to match gnome regardless of case. This is handy if you are looking for all occurences of a specific pattern