Using grep with regular expressions to isolate text data.
After completing this section, students should be able to:
Use the grep command with common options.
Use grep to search files and data from piped commands.
grep is a command provided as part of the distribution which utilizes regular expressions to isolate matching data.
grep usage
The basic usage of grep is to provide a regular expression and a file on which the regular expression should be matched.
[student@serverX ~]$grep 'cat$' /usr/share/dict/words
Because regular expressions often contain shell metacharacters (such as
$, *, and others), it is recommended practice to use single quotes
(') to encapsulate the regular expression on the
command line.
grep can be used in conjunction with other commands
using a |.
[root@serverX ~]#ps aux | grep '^student'
grep options
grep has many useful options for adjusting how it uses the provided regular expression with data.
| Option | Function |
|---|---|
-i
| Use the regular expression provided; however, do not enforce case sensitivity (run case-insensitive). |
-v
| Only display lines that DO NOT contain matches to the regular expression. |
-r
| Apply the search for data matching the regular expression recursively to a group of files or directories. |
-A <NUMBER>
| Display <NUMBER> of lines after the regular expression match. |
-B <NUMBER>
| Display <NUMBER> of lines before the regular expression match. |
-e
| With multiple -e options used, multiple regular expressions can be supplied and will be used with a logical or. |
There are many other options to grep as well, but these are some that are used frequently.
grep examples
For the next few examples, use the following file contents, stored in a
file named dogs-n-cats.
[student@serverX ~]$cat dogs-n-cats# This file contains words with cats and dogs Cat dog concatenate dogma category educated boondoggle vindication Chilidog
Regular expressions are case-sensitive by default; using the -i option
with grep will cause it to treat the regular expression without case sensitivity.
[student@serverX ~]$grep -i 'cat' dogs-n-cats# This file contains words withcats and dogsCatconcatenatecategory educated vindication
Sometimes, users know what they are not looking for, instead of what they are looking
for. In those cases, using -v is quite handy. In the following
example, all lines, case insensitive, that do not contain the
regular expression 'cat' will display.
[student@serverX ~]$grep -i -v 'cat' dogs-n-catsdog dogma boondoggle Chilidog
Another practical example of using -v is needing to look at a file, but
not wanting to be distracted with content in comments. In the following example,
the regular expression will match all lines that begin with a # or ; (typical
characters that indicate the line will be interpreted as a comment).
[student@serverX ~]$grep -v '^[#;]' <FILENAME>
There are times where users need to look for lines that contain information so
different that users cannot create just one regular expression to find all the data.
grep provides the -e option for these situations.
In the following example, users will locate all occurrences of either 'cat' or 'dog'.
[student@serverX ~]$grep -e 'cat' -e 'dog' dogs-n-cats# This file contains words withcats anddogsdogconcatenatedogmacategory educated boondoggle vindication Chilidog
grep (1) man page