CS 114: UNIX Tools
Homework 3
Due Friday, March 16, 2:30 PM
(Yes, that is the day before spring break. Assignments will happily be accepted
during my office hours from 2:30 until 3:30 on Thursday, March 15.)
Problem 1:
This problem asks you to edit a text file using either vi or emacs.
Follow the instructions in the file ~cs114/HW3/114HW3P1.txt
Problem 2:
This problem tests your ability to write UNIX regular expressions and to use the
sed utility.
Part A: Regular Expressions
In this section, just write the regular expression asked for. You do
not have to incorporate it into a sed command.
2.1: Write a regular expression that identifies URLs. In this problem, assume that all URLs start with "http://", the "http://" must be preceded by a space, and that the URL continues until the next space is encountered. You do NOT need to check that URL syntax is being used beyond this. (Motivation: think of the type of expression that might be used to do automatic hyperlink creation in programs like the Microsoft Office packages.)
2.2: Write a regular expression that matches either singular or plural occurrences of the word "tree". Make sure that your expression matches when tree occurs as the very first or very last word on a line, and that it does NOT match strings such as "street".
Part B: sed
In this section, you will now be using sed, both via its command line interface
and writing sed scripts.
2.3: Using the regular expression written in 2.1, assuming it is correct as-is, write a single line UNIX command, using sed without any scripts, that turns any occurrence of a URL (as defined in 2.1) into the html code to create a hyperlink to that URL.
Note: Given a URL such as http://www.cs.cornell.edu/ the html for a hyperlink to that URL is:
<a href="http://www.cs.cornell.edu/">http://www.cs.cornell.edu/</a>
In general, given a URL "url-text", the html for a hyperlink to that URL is:
<a href="url-text">url-text</a>
2.4: Write a sed script that takes as input a data file where each line has the following format:
Name ID# City State Phone
and makes the following changes:
For example, the following lines in such a file:
Amanda 123456 Ithaca NY 255-1181
Scott 987654 Boston MA 555-1234
would be changed to:
Amanda Ithaca NY (607) 255-1181
Scott Boston MA 555-1234
Note that all ID#'s are assumed to have 6 digits and all phone numbers are assumed to not include area codes. Fields are separated by spaces.
2.5: Rewrite your script so that
You may still assume that all fields are in the order listed in 2.4 and are separated by spaces.