What is grep?

Grep is a command included in most Linux distributions by default. It is an open source piece of software which allows you to search within a file to find instances of a particular expression.

What does grep do?

The grep command searches a text file based on a series of options and search string and returns the lines of the text file which contain the matching search string. The output can also be manipulated or piped into the console depending on what you need to do with the data.

See Also: (Live Webinar) Meet ServerMania: Transform Your Server Hosting Experience

How to use grep

Basic Syntax

At its core, you can type the command grep, followed by any options associated with the command, then the pattern within the file you want to find, followed by the file name

grep [OPTION]… PATTERN [FILE]…

For example:

The file named file2 has the following content:

catCat123Johnson

Performing the following command executes a case sensitive regular expression search of the file:

grep cat file2

Returns the following result:

cat

Grep Options

There are dozens of options to choose from when executing a grep command. Here are some of the common options:

OptionDescriptionExample
-cCounts the number of instances of the search string in the file.grep -c string filename
–colorDisplays the search results with the match search string in color.grep –color string filename
-iCase insensitive search. Returns results ignoring whether or not the letter is uppercase or lowercase.grep -i cat file2
-fObtain the pattern to search from in a file.grep -f filename file
-nPrints the line number that the expression appeared on when outputting the command.grep -n cat file2
-vReturns all lines that do not contain the search string.grep -v string filename
-EFor extended expression searches. You can also use the egrep commandgrep -E cat file2
-FPattern is a set of fixed strings. You can also use the fgrep command.greo -F cat file2
-RPerforms a recursive search of the current directory and all sub-foldersgrep -r searchstring

Pattern Options

When entering the expression to search the file for, you do not need to limit yourself to a whole or partial word. You can use various characters in order to perform more expensive or specific searches.

OptionDescriptionExample
Wildcard Search *An asterisk can be inserted anywhere in the expression in order to return all results with any character in the expression.

For example:

c*t will return all results that begin with c and end with t, with any amount of characters in the middle.

*t will return all results that end with t

c* will return all results that start with c

grep c*t file2

grep *t file2

grep c* file2

Full word searchAdding quotes around a world will ensure grep only returns full word matches rather than any words that contain the search string.grep “string” filename
Search for multiple wordsThe egrep command can be used to search for multiple words in a single grep search.egrep ‘word1|world2’ filename
Title 420$1 Piece

Outputting Grep

The output from the grep command can also be piped into another command or sent to a file depending on what you need to do with the data.

Outputting to File:

grep cat file2 > /home/grepoutput.txt

Piping into another command:

This command will look for the expression “cat” in the file named file2 and output it to the word count command wc. The -l option will then list the number of lines in the grep output.

grep cat file2 | wc -l

In Summary

As you can see, grep is a powerful tool with a variety of command options and search parameters in order to return exactly the information you need. It is one of the most useful commands for linux administrators and you will no doubt find it useful in your administration tasks.

Have a question about grep? Leave a comment below!