Homework Assignment sed - CSC250, Fall 2000

Simple exercises to try using grep and sed and regular expressions

Due December 14, 2000, midnight

You must use your class account to do this homework. The attached sheet contains the information needed for logging in. You will get the sample files from the class account handout directory and you will submit the homework electronically. You will type in answers to questions in the text below, in a file. Obtain this file by typing:
getcopy sedHWanswers
Whenever you see Question x. below, where x is an integer, put the answer in sedHWanswers. Start by putting your name and account id at the top of the file.
Type
getcopy sample1.input

This is a file with various statements about trees. Type
more sample1.input
to read it. Now type
sed 's/tree/shoe horn/g' sample1.input
As you can see, every occurrence of the word "tree" is replaced by the phrase "shoe horn." The contents of file sample1.input has not changed. In order to save the results, you could direct them to another file by typing, e.g.
sed 's/tree/shoe horn/g' sample1.input > sample1.output

Homework with sed


Question 1. Now type
sed 's/[s-t][a-h]./Judy/g' sample1.input
Explain what happened and why. Why is only the first line affected? Don't forget to use your "Crib sheet" handed out in class. Put the answers in the homework file.
Question 2. Now write a sed command that will substitute the word "button" for "tree", only if tree occurs at the end of a line (again use file sample1.input). Make sure to try it out! Put the sed command in the homework file.
Question 3. Write a sed command that will substitute the word foible for the word tree but not the word trees. Can your sed command substitute foible for the occurrence of tree at the end of the first line? Why or why not? Again make sure to try it out! Answer the questions and put the sed command in the homework file.
Question 4. Write a sed command that will replace two or more of the character e in a row with the string alala. Hint: use square brackets.
The result should be:
I think that I shall never salala a poem lovely as a tralala
Binary tralalas have dual branches
My family tralala is acyclic
A tralala is not fralala

Homework with grep

Get a copy of sample2.input by typing:
getcopy sample2.input
This is a sample log file of the type generated by an http server whenever a client requests information or runs a cgi program. Type
more sample2.input
to take a look.

Use grep or egrep in the following questions, again typing the answers into the file SedHWanswers. Make sure both sample1.input and sample2.input are in the same directory and that you are running your grep commands in that directory.

Question 5. Type
grep 'ee' *.input
How many lines match and are printed? Which of the two files have lines that match?
Question 6. Use grep to print the lines in the file sample2.input that contain "03." as part of the client hostname. There should only be 2 such lines. Since the character . is special and means any character, use \. when you want to match the actual period character specifically. Put the grep command in the homework file.
Question 7. The log file (sample2.input) also contains the time at which the URL was sent to the server. Use grep to find lines for activity that occurred between 1 and 3 in the morning. Time is denoted as :hour:minute:second and each value between colons is two digits. Note: you can use square brackets in grep as well. There are again 2 such lines. Put the grep command in the homework file.
Question 8. Use one grep command to find URLs from sample2.input that contain either .asp or .gif (there are 5) and put the grep command in the homework file.
For more information, check out the links on
http://www.cs.smith.edu/~jfrankli/250f00/sed.html
Summary: due Thursday December 14 by midnight. Submit by typing
submit sedHW sedHWanswers