Grep by Example
Regardless of what flavor of Unix, Linux or BSD you use, you can generally count on Grep, Sed and Awk's availability. These command line utilities provide different ways of searching and manipulating text. Understanding how to use these powerful tools will greatly enhance your command line effectiveness and productivity.
This will be a multi-part series, where each post will focus on one of these three tools. This segment will explore "Grep" using a nursery rhyme. To give some concrete examples, when "rhyme.txt" is referenced, assume it has the following content:
Hickory dickory dock
The mouse ran up the clock
The clock struck one
The mouse ran down
Hickory dickory dock
-- "Hickory, Dickory, Dock" (public domain)
Example 1: Searching for a Given String
If we wanted to find all occurrences of the word mouse in "rhyme.txt," we could use this command:
grep 'mouse' rhyme.txt
which would result in:
The mouse ran up the clock The mouse ran down
Note that if you had multiple files to search (ex: rhyme1.txt, rhyme2.txt and rhyme3.txt), the syntax would be:
grep 'mouse' rhyme1.txt rhyme2.txt rhyme3.text
Or using the "glob" (i.e., "wildcard") syntax:
grep 'mouse' rhyme\*.txt
Or you could even use:
cat rhyme\*.txt | grep 'mouse'
(Note: As in the above example, when you don't provide any file names, Grep will process input from standard input.)
Example 2: Using Regular Expressions
Say we wanted to find lines that rhymed (i.e., ending in "-ock"). We might be tempted to use:
grep 'ock' rhyme.txt
matching lines containing "dock" and "clock". However, it would also return the line:
The clock struck one
which is not what we want. Instead, we want to leverage regular expressions:
grep 'ock\$' rhyme.txt
which would correctly return:
Hickory dickory dock
The mouse ran up the clock
Hickory dickory dock
If we wanted to be even more precise, we could enable grep's extended regular expression syntax with the "-E" flag and use:
grep -E '(dock|clock)\$' rhyme.txt
See below for links for further reading about regular expressions in grep.
Example 3: Case Insensitivity
Now, pretend you wanted to find usages of the word "the." Using the following command:
grep 'the' rhyme.txt
will only return the line:
The mouse ran up the clock
as by default, grep matches not only the search text but the case as well. To make grep case-insensitive, use the "-i" flag:
grep -i 'the' rhyme.txt
which will correctly return:
The mouse ran up the clock
The clock struck one
The mouse ran down
Example 4: Inverting Search Results
Having obtained all lines that contain "the," the "-v" flag will invert the match, returning all lines that do not contain "the." Thus, we could use the following (remembering, of course, to use the "-i" flag):
grep -i -v 'the' rhyme.txt
which would return:
Hickory dickory dockHickory dickory dock
Example 5: Context
Lastly, Grep provides the ability to display contextual lines. For example, if we wanted to search for "struck" and display both the previous and next line, we could use:
grep -C 1 'struck' rhyme.txt
resulting in:
The mouse ran up the clock
The clock struck one
The mouse ran down
Using the "-A" and "-B" options will allow you to independently control the number of preceding and subsequent contextual lines. For example, if we wanted to search for "struck" and display the previous line, we could use:
grep -B 1 'struck' rhyme.txt
There's More!
Although these are the major options, grep has many more capabilities that we didn't have time to cover. Check out its man page for a full list.