Introduction
Announcements

Schedule
Labs
Assignments
TA office hours

Tests, exam

Topic videos
Some course notes
Extra problems
Lecture recordings

Discussion board

Grades so far

3. Write shell commands to output the following:

a) all lines from the file "staple" containing both an 'x' and a digit

b) either the word "happy" or "sad", depending upon whether or not the number in the file "nail" is greater than 50 (greater than 50 is happy) (assume that the file "nail" contains just one integer and no other data)

Solutions:

a)

grep x staple | grep '[0-9]'
Note: No need for ".*" at the beginning or end of the grep expressions as they are not "anchored" to the beginning or end of the line. No penalty for including this, except that it made it less likely that you would get a correct answer because it's more complicated -- Keep It Simple!
Note 2: Putting this in a single regular expression requires the '|' operator (which actually requires egrep rather than grep, but we'd ignore that in grading, if anyone actually got that far along in a solution using this more difficult method)
Note 3: Putting quotes around the bits of the regular expression syntax which are also shell glob syntax (square brackets or asterisks) was about half of the points for this part.

b)

if test `cat nail` -gt 50
then
    echo happy
else
    echo sad
fi


[sample midterm from Winter 2011]