Regular Expressions   «Prev  Next»

Unix Grep Command - Exercise

Using a backslash to turn off a character’s meaning

Searching with regular expressions


Objective: Use regular expressions to describe search patterns.

Exercise Scoring

This exercise occurs in two parts and is worth a total of 5 points. Part I is a hands-on practice session using the UNIX lab and Part II will be submitted. Follow the instructions below for submitting your exercise.

Background/overview

In this exercise, you will search a phone list for various patterns, using the grep command and regular expression syntax.

Instructions: Part I

  1. Log in to your course account.
  2. Type the script command to begin saving your UNIX session to a file named ex200-4: % script ex200-4
  3. Go to the practice directory, which contains the phonelist file.
  4. Use grep to perform pattern matching on the phonelist file, as in this example: % grep 'regular_expression' phonelist
  5. Use the following regular expressions in your grep commands.
    r[aio]
    ^r[aio]
    m..g
    0$
    r.*s
  6. Type exit to end the script command.
  7. Log out when you are done.

Instructions: Part II

In words, the pattern that each of the regular expressions below will match.
  1. r[aio]
  2. ^r[aio]
  3. m..g
  4. 0$
  5. r.*s

For example, if the regular expression is ^[A-Z], you would say something like: "Matches any uppercase letter at the beginning of the line."

Hints

If you enter the logout command, but it responds “Not login shell,” it means you forgot to end the script command.
Type exit and then logout.

Submitting your exercise

Type your answers directly into the text box provided or cut and paste them from a word processing program. When you have completed both Parts I & II, click the Submit button.