Corning Community College

UNIX/Linux Fundamentals


CS #8: Fun with grep




Objective

To explore the grep family of pattern-matching utilities.

Reading

The REGULAR EXPRESSIONS section of the grep(1) manual page.


Background

The grep(1) utility has been used in prior assignments in the context of "searching for" a pattern in a file.

grep wasn't just arbitrarily made up out of the blue. It has its roots in the regex functionality of vi/ex. (regex or regexp is an abbreviation of sorts for regular expression). It is actually an acronym:

GREP = Globally [search for] Regular Expression [and] Print

GREP can be used by itself on the command line or in conjunction with other commands (especially with pipes). You always need to supply your regular expression or pattern to grep.

There are a few different 'grep's in existance. Aside from the original grep, there is also egrep and fgrep.

variant of grep
description
grep
original grep. Accepts regex in search patterns
egrep
grep that accepts extended regex metacharacters in search patterns
fgrep
grep that accepts no regex. Just takes literal strings. Also called fast grep.

So what is the difference? It all depends on what you want to do. For most cases, grep will be suitable.

However, sometimes we wish to add a little more capability to our RegEx patterns. The "e" in egrep stands for "Extended Regular Expressions", and it adds some operators to our list of available Regular Expressions, including:

metacharacter
description
( )
group patterns together
|
logical OR, can be used for condensing many patterns together
+
match 1 or more of the preceding
?
match 0 or 1 preceding

The big advantage of egrep is that we can combine together patterns with an OR, allowing once again for the creation of single patterns that can apply to a wide range of situations. For example:

egrep '(Mon|Tues|Wednes|Thurs|Fri|Sat|Sun)day' somefile.text

Would match any day of the week. By utilizing the parenthesis to group together and the | to OR together, we have a single pattern that can be used to locate items of this pattern.


Exercise

Copy the pelopwar.txt file from the grep/ subdirectory of the UNIX Public Directory into your home directory.

1. Using the grep(1) utility, show me how you get the following:
a.
Find all occurances of the substring "coast"
b.
How many matches?
c.
Find all unique occurances of coast as a word (ie not as part of a word)
d.
How many matches?
e.
Find all occurance of the word Dorian that are the last word on the line.
f.
Find all lines that begin with the word Again

NOTE: Be sure to count the words instead of relying on 'wc -w'. Why would this make a difference?

2. Using egrep(1), show me how you get the following:
a.
All instances of Athens or Athenians that occur at the beginning of the line.
b.
Accomplish this with one call to egrep using extended regexp's (use your book)
c.
All instances of Corinth or Corinthians that occur at the end of the line.
d.
Accomplish this with just one call to egrep

3. Using fgrep(1), show me how you get the following:
a.
Try the same regexp that you used in 2a
b.
Did you get any results?
c.
Why did it turn out this way?

4. Create and show me the command-lines to:
a.
Display the first 4 lines of the last command's output that begin with any of your initials. Include your output too.
b.
Using the sort(1) utility, alphabetize the output of the lastlog command and use grep to search for all lines that begin with a lowercase e through g, then print out the first 4 lines of the resulting output.
NOTE: For #4b, the head(1) utility can be used to help format the output.

Conclusions

All questions in this assignment require an action or response. Please organize your answers into an easily readable format and be prepared to submit the final results to your instructor.

Your assignment is expected to be performed and submitted in a clear and organized fashion- messy or unorganized assignments may have points deducted. Be sure to adhere to the submission policy.

When complete, electronically submit your assignment using the "Assignment Submitter", located here:

http://lab46.corning-cc.edu/haas/spring2009/unix/submit/submit.php?cs8

As always, the class mailing list is available for assistance, but not answers.

Last Updated: October 27, 2008