| The grep Utility is closely
related to the "egrep" and "fgrep"
utilities that we will not discuss here. The other two versions of
"grep" have other capabilities but you will most likely never need them as a
web technician. Of course, you can always check them out using the
"man" utility as discussed on the first day of this tutorial. |
As you might expect, grep accepts several useful options
that are discussed in the table below:
| Option |
Explanation |
| -b |
Prints the matching line with the block number |
| -c |
Displays the number of matching lines |
| -e pattern |
A special type of pattern match for when the pattern starts with
a "-" character |
| -f filename |
Instructs grep to use the pattern defined in the given file. |
| -h |
Censors the file name from the display |
| -i |
Matches case insensitively. |
| -l |
Displays the list of filenames with matching content only (not the lines) |
| -n |
Displays the matching line numbers as well as the line |
| -s |
Censors error messages |
| -v |
Displays non-matching lines |
Let's look at a few common examples of how
to use grep.
Suppose you changed your email address and wanted to
find all the web pages in a given directory that reference the old
address so that you could modify them. In the following example, we
search for selena@old.com and find two files we need to modify. Can
you imagine how long it would take to search each file by hand!!!
|