|CSC 161||Grinnell College||Spring, 2010|
|Imperative Problem Solving and Data Structures|
This lab continues our exploration of Linux, including pipes, filters, additional Linux utilities, wildcards, quoting, and command substitution.
Before starting work in the lab, be sure you have read the following materials:
Explore the use of the ls command to obtain a long directory listing.
Type ls -l in a terminal window. Describe what information is displayed and how the data are organized.
In a terminal window, type man sort to obtain information about the sort command. Based on this information, determine what the command sort -k 6 does.
Type the command ls -l | sort -k 6 in a terminal window. Again, describe what information is displayed and how the data are organized.
Type the command ls -l | sort -k 6 | more in a terminal window. (You can scroll through the data page at a time by hitting the space bar.) Explain the role of the last pipe | more in this line.
The following table is repeated from the reading for today's lab. All of these filters can accept their input from regular files, stdin, or a pipe. None of them modify their original input; rather, they generate new output that reflects a modified version of the input.
|wc||"word count" - counts characters, words, and lines in input||wc -l ~/.bashrc|
|sort||sorts lines in input||sort -k2 ~coahranm/share/csc201/sciencefac.txt|
|uniq||"unique" - removes (or reports) duplicate lines in input||uniq ~coahranm/share/csc201/duplicates.txt|
|grep||searches for a target string in input||grep li ~coahranm/share/csc201/duplicates.txt|
|cut||removes parts of lines from input||cut -d' ' -f2 ~coahranm/share/csc201/sciencefac.txt|
|diff||reports the differences between two files||diff ~walker/public_html/courses/153.sp09/labs/lab-linux-c.shtml ~walker/public_html/courses/153.sp09/labs/lab-linux-c.shtml~|
Use the filters given above (and other utilities if needed) to perform the following tasks. Note that for some of the tasks you may need to combine filters with pipes.
Count the lines of source code in a program you wrote earlier this semester for this course.
Determine the number of user accounts on the MathLAN. Recall that each account has a directory in /home.
Print a list (in the terminal window, not on a printer) of 2007-2008 faculty in Grinnell College's Science Division, sorted by last name. The file ~coahranm/share/csc201/sciencefac.txt contains the data you need.
Print a list of 2007-2008 faculty in the Biology Department. Your list should not include faculty in any other department.
Consider one of the programs you wrote earlier this semester. Take a quick look at it, using less to remind yourself of a variable name that is used in several places in the file. Now use grep to print a listing of the lines that include that variable. Get grep to print the line number (in the source file) for each line of output as well.
Note that it can be very useful to use grep in this way when you return to a project after taking a long break from it. For example, you might want to find every instance of a given class -- in any source file in the project -- as part of re-acquainting yourself with your code.
Print a list of all 2007-2008 Grinnell faculty named David.
Hint: To do this, it would be helpful to create a single list that combines all the entries in the three faculty lists I have provided. But instead of generating a separate combined file, you can do this on the fly using cat as shown below. This is where cat gets its name -- from its ability to concatenate multiple files.
cat ~coahranm/share/csc201/socialfac.txt ~coahranm/share/csc201/humanfac.txt ~coahranm/share/csc201/sciencefac.txt
Print a unique list of departments in the Humanities Division.
The names provided in ~coahranm/share/csc201/ give the 2007-2008 faculty for the various departments, and the Chair of each department is denoted by an asterisk. Use grep to output a list of Department Chairs in one (or all) of the divisions. It might also be nice to sort your list by last name.
Ms. Coahran has the following file in her share directory:
~coahranm/share/csc201/goofy file name
What happens if you try to list its contents using the following command? Give it a try to be sure.
cat ~coahranm/share/csc201/goofy file name
Modify the above command in at least two ways to obtain the desired listing.
Try command substitution with backquotes, by looking up any of the following commands you are not familiar with already.
echo There are `ls | wc -l` files in my current working directory. echo Today is `date +%A`. It is now `date +%r`.
This document is available on the World Wide Web as
created January 2007 by Marge Coahran
revised January 2008 by Marge Coahran
revised 10 April 2008 by Henry M. Walker
revised 3 April 2009 by Henry M. Walker
reorganized 2 April 2010 by Henry M. Walker
last revised 3 April 2010 by Henry M. Walker
|For more information, please contact Henry M. Walker at firstname.lastname@example.org.|