CSCI 3294 (Unix Power Tools), Spring 2005:
Homework 5

Assigned:
March 28, 2005.

Due:
April 11, 2005, at 5pm.

Credit:
20 points.

Reading

Read about the following topics in the list of suggested readings, either in one of the suggested readings or in another book or reference. (These are the topics for March 21):

(Notice that questions below are grouped by topic, so you can read about one topic, do the related questions, and then move on to the next topic.)

Problems

Answer the following questions. You may write out your answers by hand or using a word processor or other program, but please submit hard copy, either in class or in my mailbox in the department office. Answers to most questions will involve experimentation on a UNIX or Linux system. You are free to use any appropriate system; if you use something other than Red Hat Linux please tell me what.

Regular expressions

  1. (4 points) Write a grep command to search all the files in the current directory for lines containing either your last name or your pet's name. (If you don't have a pet, make up a second name.) (You don't have to also search subdirectories, just files in the current directory itself.)

  2. (4 points) Write a grep command to search all files in the current directory for strings of the form CSCI-xxxx, where xxxx is four decimal digits.

  3. (4 points) Suppose you have a C program in file foo.c that uses variable x as an all-purpose integer loop counter. Write a sed command that will rename this variable to i everywhere it occurs but will not change other occurrences of the letter x. (Note that essentially the same command would work in vim. Don't worry about saving the output back into the file; the point here is the command to make the change.)

  4. (8 points) Suppose you have a text file (call it foo) containing a list of names and phone numbers. Each line contains first a phone number (consisting of any combination of digits, dashes, parentheses, and spaces) and then a name (consisting of any combination of uppercase letters, lowercase letters, and spaces). At least one space separates the name from the phone number. Write a sed command to reverse the names and phone numbers, leaving at least one space between the name and phone number. For example, ``555-1212 Generic Phone Number'' would be changed to ``Generic Phone Number 555-1212''. (Hint: Backreferences may be useful.)



Berna Massingill
2005-03-28