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:
1
|
|
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:
1
|
|
Or using the “glob” (i.e., “wildcard”) syntax:
1
|
|
Or you could even use:
1
|
|
(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:
1
|
|
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:
1
|
|
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:
1
|
|
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:
1
|
|
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:
1
|
|
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):
1
|
|
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:
1
|
|
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.
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.