Due date: Monday, October 22 at noon
Late policy: If the homework is submitted before the due date, you get 100% of your grade; if the homework is submitted before the following wednesday, 5am, you get 50% of your grade. Later than that, you get nothing.
This homework will be graded out of 15 points.
Please read all the instructions carefully. Remember, everything is case-sensitive!
You should do all the work yourself without any assistance from other people. If you need help, post your question to cornell.class.cs114 newsgroup or if you want to keep your question private, e-mail me at cs114@cs.cornell.edu. Please use the newsgroup whenever possible. Questions posted to the newsgroup will be answered much sooner.
(1 point) Create a subdirectory cs114-HW2 in your home directory. Make sure you are the only one who has any kind of access permissions to it.
(0 points) Go to ~cs114/HW2 directory. It has a subdirectory set up for each of the CS114 students. Copy all the files from ~cs114/HW2/your_netid to your cs114-HW2 directory. (In case you are wondering - these files contain lines semi-randomly picked from a bunch of man pages).
For this part, use Emacs (which you can invoke using emacs). Some of these problems are quite tricky, so triple-check your results. You may find the diff utility useful for checking what happened.
(2 points) In file1, replace the first character of
every line by ZZZ and a space in lines 10 through the
end of the file. For
example, you would change the line
xyz
into
ZZZ yz
Hint: Look at the Emacs
command replace-regexp (with M-x
describe-function). It takes a regular expression
and something to replace it with. It replaces all occurrences of
the regular expression in the file, from the point where the
cursor is. How do you position the cursor to line 10? (Look at
the mode line carefully.) How can you recognize the beginning of
a line with a regular expression?
(2 points) In file2, replace each digit between 1 and
5 (inclusive) with two symbols [].
Example::
12ab8rs5dA
should become [][]ab8rs[]dA.
(3 points) In
file3, change all the lines that contain at least one
| symbols in the following way: replace the
first | by -|-
Try doing this using a single replace-regexp
command.
Example: qWe|rtY|uio should become
qWe-|-rtY|uio
Hint:You will need to look at
some documentation about regular expressions to answer this
problem (for instance here, or any of the links I provide on the web page),
and experiment a little bit. What does \(xy\)\1 match?
What does [^abc] match?
(0 points) Using the man command on babbage, read the grep man page and find out, how to tell it to count the number of lines matched.
(0 points) Create a file named answer in your cs114-HW2 directory.
(3 points) Count the number of lines in file4 that
have at least 3 occurences of some 2-character string (each line
can have its own 2-character string). Put this number on the
first line of the answer file (don't put anything else
onto that line).
Example: qw1~wt1~aaass1~xce should be counted because
1~ appears 3 times.
Hint: Recall the regular
expression constructs you had to use in Emacs above.
(1 point) Same as the previous question, but when the first of the two symbols in that string is not a lower-case a. Write the number in the second line of the answer file.
(3 points) In the file5, count the number of lines that either
To submit your HW, run ~cs114/bin/grade-hw2. The script will work as many times as you run it, but you will only receive credit for your first submission. If something goes wrong, please let me know ASAP.