CS197U: A Hands-on Introduction to Unix

Assignment 2: Working with files

This assignment will teach you about working with files and using the utilities covered in Lecture 2.

Part 0: Making a directory for your work (1 pt)

First you must make a copy of the assignment files in your course directory. To do this:

1. Start in your home directory on an edlab machine (type ''cd'' to jump directly there)

2. Use cd cs197u to move into your course home directory.

3. Use cp -r /courses/cs100/cs197u/cs197u/assignments/assign-2/ . to copy the assignment directory into your course home directory.
(the "." in the cp command is important!)

4. Change into the newly copied ''assign-2'' directory.

5. Type pwd and verify that it says: /nfs/elsrv4/users4/grad/USERNAME/cs197u/assign-2 or /courses/cs100/cs197u/USERNAME/assign-2

6. Use ls to verify that the directory contains 3 files.

Part 1: Short answer (5pts)

In your new directory, use vim to edit the file named answers.txt. Fill in the answers to the questions in Part 1 as described in the file. There should be a list of nine commands to try, and one other question about head and tail.


Part 2: Parsing files (4pts)

In Lecture 2, we learned how to use pipes to combine multiple commands together. We also learned how to use grep to search through a file and sort to sort files alphabetically or numerically. The command wc [file] is used to count the number of words, lines, or bytes in a file. Use man wc to find out more about this command.

2.A) Use wc to find out how many lines are in the file romeoandjuliet.txt.

2.B) How many lines in the romeoandjuliet.txt file contain the word "Romeo"?

2.C) What is the line number of the 100th line in the romeoandjuliet.txt file that does not contain a comma? Write the line number, the line, and the command you use into answers.txt. Hint: Find the flag for grep that displays the line number in the output. In your solution, the line number and the actual line from the file can be in the same output.

Next, we will analyze the file utah.txt. This is a data file with three comma separated columns: Population, Year, County name.

2.D) Use grep, sort, and one other command we learned about to print out the five counties with the greatest population (in decreasing order) from the year 1969. Write the command you use into answers.txt. Hint: the 5th most populous county should be "Cache", and you may need to give sort 2 different flags.

2.E) Save the output from the previous command (using the > symbol) to a file called top-counties.txt.

2.F) Read about the cut command in man and learn how to separate a column (or `field') from a file. Use cut with 3 other commands to find out the unique number of counties that Utah has. Write the unique number of counties and the command you use into answers.txt.


I will collect the files answers.txt and top-counties.txt directly from /courses/cs100/cs197u/USERNAME/assign-2 . Make sure you followed the directions in part 0 so the files exist in the correct location.

To check this, use the following commands: