Summary: In today's lab, you will explore regular expressions in the Unix environment.
Collaboration: Feel free to work on this lab in pairs or trios.
Turning It In: Save your answers in a plain text file and submit it using the ECA.
Grading: I expect that you will gain more from doing this lab than from me grading this lab. I will simply scan through your answers to see if you had any particularly valuable insights.
In this lab, you'll use regular expressions to look for English words
that match particular patterns. You can find a list of English words
The standard command for searching using regular expressions is
grep (and no, it is not "GNU rep").
stands for something like
general regular expression patterns.
You traditionally use
grep in a form like the following:
% grep 'regexp' file
It is important to put the regular expression in single quotes so that the shell does not interpret special characters, like braces, parens, and stars.
Here are the basic mechanisms for building a grep-style regular expression. You should be able to find more in the man page.
ab: concatenation without parens
[abc]: alternation of single characters
[^aeiou]: all but vowels
*: zero or more copies
.: any character
^: start of line
$: end of line
What can you do with the output from
grep? You can put
the results in a new file.
% grep 'regexp' file > results-file
You can look
at the results by piping them through
% grep 'regexp' file | less
You can simply count the results by piping them through
the word-count program.
% grep 'regexp' file | wc -l
You can even send the results through another invocation of
(which is a nice way to get values accepted by both regular expressions;
that is, the interesection of two langauges).
% grep 'regexp1' file | grep 'regexp2' | ...
Questions: Write a regular expression for each of the following and determine how many words in the Unix dictionary match the regular expression.
Thursday, 1 February 2001 [Samuel A. Rebelsky]
ad hoclab (narrated the content to the class).
Monday, 9 September 2002 [Samuel A. Rebelsky]
Tuesday, 10 September 2002 [Samuel A. Rebelsky]
I usually create these pages
on the fly, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Wed Feb 4 20:15:21 2004.
The source to the document was last modified on Wed Feb 4 20:15:19 2004.
This document may be found at