Scripts and Utilities -- Sed/Ed lab



Write the following shell scripts. When you're done, submit them using the 300_submit script. In your shell scripts, you may only use the following: You may not write any C programs to help yourself out, nor are you allowed to use any other Unix programs (like awk or perl) in your scripts.

Finally, your program should handle errors gracefully. For example, if the command line arguments are supposed to be files, and the user specifies a non-file, you should print a descriptive error message on standard error, and either exit or not exit as you see fit.

Required programs

  1. reverse: This takes lines of text on standard input and prints them out in reverse order. That is the text on each line is UNCHANGED but the last line in the input is the first line output, next-to-the-last line in the input is the second line in the output, etc. So if the input is
    The quick brown
    fox jumped over the 
    very lazy and
    anemic blue-tick
    liver-spooted sad and lonely
    hound dog.
    
    the output would be
    hound dog.
    liver-spooted sad and lonely
    anemic blue-tick
    very lazy and
    fox jumped over the 
    The quick brown
    
    HINT: You can add things to the lines to help change their order so long as you delete those things before you print the lines out. This will take utilities from the previous lectures, as well as this one, to solve.

  2. nswitch takes a list of names in the form
    last_name, first_name 
    
    on standard input, and prints them out as
    first_name last_name
    
    on standard output. You can use the example_name_file as an example input file. (Hint: use subpatterns)

  3. Suppose you are running a pool over the Internet. People email you entries. The entries are simply supposed to be a list of teams, one per line. The teams are: Washington, Philadelphia, New York, New Jersey, Boston, Atlanta, Cleveland, Indiana, Milwaukee and Charlotte.

    Even though you instruct the participants to email you the list in that format, people always do irritating things, like list multiple teams per line (separated by a comma), abbreviate teams, put numbers before or after the team to denote where they should go on the list, etc.

    Your job is to write the shell script fixit, which takes an email file on standard input, and produces the correct format (just a list of 10 teams, spelled correctly) on standard output. There are 7 sample email entries in the files entry1 through entry7. Your program should fix them all!

Recommended programs

  1. unhtml takes a file on standard input and strips out all the html tags (anything inside < and >). You may assume that all html tags begin and end on one line, and you don't have to deal with < and > characters that are not part of html tags. Print the ones not part of the tag to the screen.

  2. mail_strip: This takes a mail file on standard input or a mail file on the command line. The format of mail files is pretty simple. Each message contains a header and a body. The header is denoted by a line of the form
    From email_address other-stuff
    
    and ends with a blank line. Following the header is the body, which can contain any text except a line beginning with the word ``From''. The header of the next message follows the body of the previous message.

    mail_strip takes each mail message and puts it into a separate file. The file name for each message should be the email address of the sender. If there are multiple messages from the same sender (or if the file with the sender's name already exists), then append the message to that file. mail_strip should not destroy the original mail file.

    mail_strip should detect if a file is not a mail file.

    The version of mail_strip in my example programs directory is a C program. This is because there would be problems with me creating files in your directory if I used the same trick as I usually do. The result is that my mail_strip works blazingly fast. Yours can be much, much slower.

    You can find an example mail file in example_mail_file. When you run it, it should not make files called bigbill@ovaloffice.gov or mjordan@bulls.com.

    This was the only program where I used ed in the shell script.


Working Examples

A working example of mail_strip is in the lab directory.