Regular Expressions
Due: End of Friday Lab
Appendix A
Read Appendix A and do these exercises:
- In class we developed a regular expression in vi that would exchange
the first two fields of the passwd file. Adapt this expression for use in the sed
filter, so that you can successfully execute:
$ sed -e '...your expression here...' /etc/passwd | more
$ ypcat passwd | sed -e '...your expression here...' | more
The output should have the first two fields of the passwd file exchanged.
Modify the regular expression to exchange the second and fourth fields and omit the
third field.
Modify the regular expression to produce only the first and fifth fields, with the fifth
field first in the output.
Modify the regular expression to produce mailing addresses using the userid and the name
field. (You did this using awk earlier.) The output should be
of the form:
ian@algonquincollege.com (Ian D. Allen)
decb0012@algonquincollege.com (Smart Student)
abcd0001@algonquincollege.com (Student, Brainy)
Don't worry about fixing the last name / first name order problems that some names
have. Just use the name as it appears.
Develop a regular expression that will match and exchange the last and first names of
students in the passwd file who have last names before first names, where the names are
separated by a comma. Use the sed filter to apply this expression
to the student password entries: $ ypcat passwd | sed -e '...your expression here...' | more
The output should be the passwd file with all names appearing in
"Firstname Lastname" order, without commas.
Note how the sed program passes its input to its output unchanged if the
regular expression doesn't alter it. Your regular expression only needs to match and fix
the "Lastname , Firstname" fields; it doesn't have to match or deal with the
lines in the password file that are already correct; the correct lines will pass through
unchanged.
Combine question 4 with question 5 to produce mailing address lists where all the
students have their first names before their last names (fixing the problem where some
students have last names first):
ian@algonquincollege.com (Ian D. Allen)
decb0012@algonquincollege.com (Smart Student)
abcd0001@algonquincollege.com (Brainy Student)
You might do this in one regular expression; or, you might find it simpler to run the
passwd file through a "name exchange" filter first, then through a "mail
address" filter using Unix pipes.
Hand in your command lines along with the first few lines of output from each command
line. (Don't submit the entire password file, please. Give just enough output
to show that your regular expression works.)
|