CS 2204: Homework #2. Solution sketches
Write egrep commands to operate on an input file and
- (2 points) Find all lines containing US postal abbreviations for states (you can assume that a postal abbreviation is two uppercase letters).
egrep -n "[[:space:]][A-Z]{2}[[:space:]]" sample.txt
- (1 point) Find all lines listing US city references. A US city reference is like ÔPortland, ORÕ, i.e., a word, followed by a comma, a space, and then a two-letter state abbreviation.
egrep -n "[A-Z][a-z]+, [A-Z][A-Z]" sample.txt
- (2 points) Find all lines listing university courses. A university course is a word, followed by a space, followed by exactly four digits.
egrep -n "[A-Za-z]+ [0-9]{4}[^0-9]" sample.txt
- (1 point) Find all lines containing formatted dollar amounts. These begin with a Ô$Õ, followed by a whole dollar amount (i.e., a succession of one or more digits), then a Ò.Ó, and finally, exactly two digits denoting the cents.
egrep -n '\$ *[0-9]+\.[0-9][0-9]' sample.txt
(notice that here we use single quotes to prevent the shell from interpreting
the $ sign!)
- (2 points) Find all Russian last names in sample.txt. Russian names end in 'ev', 'ov', 'off", or 'in'.
egrep -n '[A-Z][a-z]+(ev|ov|off|in)[^a-z]' sample.txt
- (1 point) Find all blank lines, i.e., lines containing nothing.
egrep -n '^$' sample.txt