CS 2304--UNIX: Assignment 6

Due Wednesday, Apr. 23, by 5pm

120 Points




A Tool For Global Searching/Replacing

Your assignment is to write a shell script called greplace (for "global replace") that will search for a given string and replace it globally in all files contained in a given directory.

greplace expects three arguments:

    greplace search-string replace-string [ directory ]
The "search-string", is a word or phrase to search for, and "replace-string" is what each occurrance of the search-string will be replaced with.

The optional "directory" parameter indicates the directory to perform the operation in. If no directory is specified on the command line, greplace should do its work in ".", the current working directory.

For every file that greplace operates on, say "file.txt", the original file should be renamed to preserve it in case the user is unhappy with the results. For example, the file "file.txt" should be renamed to "file.txt.orig" to preserve it. The results of performing the search and replace operation can then be safely placed in a new file called "file.txt" without overwriting the original.

As an example, consider the following sample session:

    % ls
    file1.txt           file2.txt

    % cat file1.txt
    Now is the time for all good men
    to come to the aid of their country.

    % cat file2.txt
    Reading manuals is tormenting!

    % greplace "men" "people"

    % ls
    file1.txt           file2.txt
    file1.txt.orig      file2.txt.orig

    % cat *.txt
    Now is the time for all good people
    to come to the aid of their country.
    Reading manuals is torpeopleting!

Please comment your script thoroughly to explain how it works.

Special Cases

greplace should work even if the replacement string is the empty string (e.g., ""). Effectively, this will delete all occurrances of the search string.

greplace should not crash if there are no files in the specified directory.

For Those In Need of a Challenge

If you would like, think about what changes, if any, would be needed so that your version of greplace worked on regular expressions instead of just strings of characters.

You might also think about how you could write your greplace command so that it performs its operations on every file in the entire directory tree (recursively) rooted at the specified starting directory.

If you would like, think about how to design your script so that it optionally accepts one or more file matching patterns on the command line after the directory name, indicating which files should be operated on. For example, the following command would search for "BOOLEAN" and replace it with "bool" globally in all C++ source and header files:

    greplace BOOLEAN bool . "*.cpp" "*.h"
Note: why are quotes placed around the last two arguments in the command line above?

Submitting Your Answers

You are to hand in your assignment by sending an email message to the address cs2304u@ei.cs.vt.edu. To receive credit, you mail message must be RECEIVED by this account by the time and date listed above. It is your responsibility to successfully submit your assignment via email.

The body of your message must be a plain ASCII text file that contains the following:

Remember that no late assignments are accepted.




Stephen Edwards <edwards@cs.vt.edu>
Last modified: Wed Apr 9 14:12:33 EDT 1997