Skip to main content
Older versions of Internet Explorer will not support certain site features. Chrome, Safari, Firefox, and Edge will provide the best experience.
Spok

Grep

Grep is a command line tool used for searching ascii text files for data sets matching a regular expression.  Grep was developed for the Unix operating system, but has since been adapted and distributed to all platforms including Linux, Windows, and Mac OS X.

Usage

Grep searches files specified as arguments, or a program's output when piped to the output of that command.  By default, it reports matching lines on standard output, but specific modes of operation may be chosen with command line options.

A simple example of a common usage of grep is the following, which searches the file fruitlist.txt for lines containing the text string apple:

grep apple fruitlist.txt

Matches occur when the specific sequence of characters is recognized, for example, lines containing pineapple or apples are printed irrespective of word boundaries.  However, the search pattern specified as an argument is case sensitive by default, so this example's output does not include lines containing Apple (with a capital A) unless they also contain apple.  Case-insensitive matching occurs when the argument option -i (ignore case) is given.

When using grep against a program's output (for example, catting a file or checking the process list), the grep command returns all lines with the matching pattern:

[amcom@db1 ~]$ ps -ef | grep postfix
root      3771     1  0 Jan14 ?        00:00:01 /usr/libexec/postfix/master
postfix   3785  3771  0 Jan14 ?        00:00:00 qmgr -l -t fifo -u
postfix  20638  3771  0 14:02 ?        00:00:00 pickup -l -t fifo -u
amcom    20828 20753  0 14:13 pts/0    00:00:00 grep postfix

Multiple file names may be specified in the argument list. For example, all files having the extension .txt in a given directory may be searched if the shell supports globbing by using an asterisk as part of the filename:

grep apple *.txt

Regular expressions can be used to match more complicated text patterns.  The following prints all lines in the file that begin with the letter a, followed by any one character, followed by the letter sequence ple.

grep ^a.ple fruitlist.txt

The name of grep derives from a usage in the Unix text editor "ed" and related programs.  Before grep existed as a separate command, the same effect might have been achieved in an editor:

ed fruitlist.txt
g/^a.ple/p
q

where the second line is the command given to ed to print the relevant lines, and the third line is the command to exit from the editor.

Like most Unix commands, grep accepts options in the form of command-line arguments to change its behavior. For example, the option flag l (lower case L) provides a list of the files which have matching lines, rather than listing the lines explicitly.

Selecting all lines containing the self-standing word apple, i.e. surrounded by white space or hyphens, may be accomplished with the option flag w.

Exact line match is performed with the option flag x.  Lines only containing exactly and solely apple are selected with a line-regexp instead of word-regexp:

cat fruitlist.txt
apple
apples
pineapple
apple-
apple-fruit
fruit-apple

grep -x apple fruitlist.txt
apple

The v (lower-case V) option inverts the sense of the match and prints all lines that do not contain apple, as in this example:

grep -v apple fruitlist.txt
lemon
lime
strawberry
banana
pear
peach
orange
mango
coconut
papaya
watermelon

The c option does a count on the amount of times the expression appears in the file and outputs that count to the screen.

grep -c apple fruitlist.txt
6

Advanced Usage

How to grep a file for 2 expressions needing to match.

Find results for either A or B

cat fruitlist.txt | grep -P "\b(apple|fruit)\b"
apple
apples
pineapple
apple-
apple-fruit
fruit-apple
orange-fruit
fruit-orange

Find results for both A and B

cat fruitlist.txt | grep -P "\apple\b" | grep -P "\fruit\b"
apple-fruit
fruit-apple

Print results with leading (B) and trailing (A) number of lines before or after the matching line(s)

grep -B 5 -A 5 peach fruitlist.txt 
--
lime
strawberry
apple
banana
pear
peach
orange
mango
coconut
papaya
watermelon
--