CSC 271
Homework 2

Assigned 22 Sept; due 11 Oct

For each of the following problems, I'd like to see not only the command or alias you came up with, but also one or more tests. To show me your tests, I suggest starting a shell session whose output goes to both the screen and a file (bash | tee logfile), typing the various commands you want to demonstrate, then exit and e-mail me the log file.

  1. Imagine that you're building a big Web site with lots of pages. Some of the pages are in .html files, some in .shtml files, and some in .php files; they're all in the same directory.

    Write a Unix command to list all the files in a specified directory whose names end in .html, .shtml, or .php.

  2. Let's change the problem slightly: now the pages are all scattered throughout a main directory and its subdirectories, sub-subdirectories, and so on.

    Write a Unix command to list all the files in a specified file hierarchy whose names end in .html, .shtml, or .php.

  3. Write a bash alias, shell function, or shell script (your choice) named pages for this command. It should take in the directory as its first and only parameter.

  4. Imagine that many of these Web pages refer to a page named snark.html, which you've decided to rename as boojum.html. Naturally, all the references will need to be fixed. We could do this with vi, but we don't want to waste time looking through the hundreds of files that don't even mention snark.html.

    Write a Unix command to edit (using vi) all the files in a specified file hierarchy whose names end in .html, .shtml, or .php and whose contents contain the name snark.html. (Hint: first figure out a Unix command to simply list the pathnames of the relevant files.)

  5. Write a bash alias, shell function, or shell script (your choice) named vipages for this command. It should take in the directory name as its first parameter, and the name of the file you're looking for (snark.html in the above example) as its second parameter.

  6. As the number of Web pages in the site grows, editing all this stuff by hand with vi gets pretty tiresome.

    Write a Unix command that automatically goes through all the HTML, SHTML, and PHP files in the file hierarchy that refer to snark.html, and replaces each such reference with boojum.html.

  7. Write a bash alias, shell function, or shell script (your choice) named substpages for this command. It should take in the directory name as its first parameter, the old name (snark.html in this example) as the second parameter, and the new name (boojum.html in this example) as the third parameter.

  8. Write a Unix command to show all the lines in a specified file that do not contain the string "-option". (This can be done either with sed or with grep. Try both approaches.)

  9. Write an alias, shell function, or shell script to extract the "useful information" from an online movie listing page like this one and present it in a useful plain-text form. Ideally, there would be a line for each showing of each movie, containing all the essential information about that movie and showing. Also write a second alias, shell function, or shell script to format this information nicely for a human reader (perhaps multiple lines per showing). You should be able to (for example) pipe the first of these into grep to find all the movies starting at 4:15, then pipe that into the second to get a nicely-formatted list.

  10. Write a bash alias, shell function, or shell script (your choice) named freq which reads words from stdin, counts how often each one occurs, and produces output in which each line is a word followed by how many times it occurred in the input. The lines of output should be sorted in decreasing order of frequency. (You should be able to do this using only standard Unix commands, not writing any C or C++ code.)
    Hint: It may be easier to do this as a shell script or a shell function, rather than an alias for a pipe. You can do it as a shell script using fairly simple filters; you can do it as a single pipe using awk.
    For those of you who have taken Dr. Siegfried's courses, another name for this might be "concordance"....


Last modified: Tue Oct 14 13:26:18 EDT 2008
Stephen Bloch / sbloch@adelphi.edu