----------------------------------------------- Unix Shells by Example: Chapter 3 Reading Guide ----------------------------------------------- -Ian! D. Allen idallen@idallen.ca Here is a reading guide and some review questions for Chapter 3 in your Quigley text: "Regular Expressions and Patern Matching". Remember to read the text_errata.txt file (under Notes) and correct all the mistakes in this Chapter. Note: The information in Chapter 3 is partially duplicated in Section 4.1.3 on p.83. Read them together. Warning: Do not confuse the meaning of metacharacters used in regular expressions and those used in shell GLOB patterns. The same characters are used; but, they often mean different things. *) Are forward slashes part of the pattern in a regular expression? (p.67) *) What would I type in VI to change any occurrence of hal9000 containing any lower-case letters to all upper-case letters in the first 10 lines of the current file? (p.68) (Make sure it fixes hal9000 and hAl9000.) *) Note: In recent versions of vim, the vim option "hlsearch" can be set to cause vim to highlight all the matches made by a regular expression. You can use this feature to see what characters are matched by the expression: :set hlsearch Warning: In hlsearch, just matching /^/ incorrectly shows the first character as being selected. (It is trying to show you that the beginning of line is being matched.) To really match the first character in a line, you must use the regexp: /^./ *) Memorize all of Table 3.1 p.69/70. You will need to know how to use all these regexp metacharacters in VI. Everything in this table is supported by the VI editor. Some metacharacters don't work as shown (or don't work at all) in sed, grep, and awk (or gawk). *) Which of the following lines are matched by this regular expression (HINT: read this question into VI and set "hlsearch" to see which patterns are highlighted): /^ *[Aa]lgonqu*in *[Cc]ol*ege *$/ Algonquin College my Algonquin College Algonqin College Algonin College Algonqin Colege AlgonqiN Colege algonqin colege algonqn colege algonqin colllege ALGONQUIN COLLEGE algonquuuin college Algonquin College campus *) In a Regular Expression (regexp or RE for short) a period matches any one character. What is the equivalent character in a shell GLOB pattern? *) In a shell GLOB (wildcard) patterh, an asterisk matches zero or more of any character. How do you do the same thing in a regular expression? (Hint: it takes two characters in a regexp.) *) What kinds of text strings do the following RE's match: /.*/ /a.*z/ /.*m/ /m.*/ /^a.*/ /.*a$/ *) The following two regexp give identical results when used by grep to select lines: $ grep '^a' /etc/passwd $ grep '^a.*' /etc/passwd They don't work the same way when used in VI on the same data: :r /etc/passwd :1,$s/^a/XXX/ :1,$s/^a.*/XXX/ Explain why the two programs have different results. *) Study well all the examples in this chapter. Type them in, set hlsearch, and try them. *) What is the difference among these regexp: /^[a-z]/ /[^a-z]/ /[a-z^]/ /[a-z]^/ /^[^a-z^]^/ Which of the following text strings do the above regexp match? (Move all the following strings to the left margin to test them.) dot.combust hat^trick A^TRAIN 99^99 alps *) What does this regexp match in a text file similar to that on p.72? (Note carefully where the spaces are in the RE.) /[a-zA-Z][.?!] */ What does this do to a text file in VIM? :1,$s/\([a-zA-Z][.?!]\) */\1 /g Note the placement of the \( and \) parentheses and the use of \1 . *) EXAMPLE 3.13 is silly. Nobody would write that, since the string on the left is fixed. It can better be done using: :1,$s/square and fair/fair and square/g Don't make it harder than it looks. Some better examples follow: --------------------------- Regular Expression Practice --------------------------- For each of the exercises below, save a fresh copy of the Algonquin Linux Lab NIS passwd file into a file: $ ypcat passwd >myfile.txt *) Use a regular expression substitution in VIM to remove all text after the last colon on each line. Don't remove the colon itself. (Hints: The right-hand side of a substitution can be empty, causing the matched characters on the left to be replaced with "nothing", e.g.: 1,$s/apple sauce// ) Remember to get a fresh copy of the passwd file for each exercise! *) Use a regular expression substitution in VIM to add the text /bin/bash to the end of every line that ends in a colon. (Hints: Escape the slashes in /bin/bash with backslashes.) *) Use a regular expression substitution in VIM to change all home directores from /home/foo to /var/foo/home on every line. *) Use a regular expression substitution in VIM to reverse the orders of letters and digits in the first field in the file, so that abcd0001 becomes 0001abcd, on every line. (Assume that letters always precede digits in the userids.) (Hints: See EXAMPLE 3.12, 3.13 and their following EXPLANATION.) Hint: Match letters followed by digits, remember each match using the special \( and \) parentheses, and swap them (.../\2\1/). (p.78) *) Use a regular expression substitution in VIM to swap the first two fields in the file, so that abcd0001:x: becomes x:abcd001: on every line, no matter what text is in the two fields. Hint: To match each field, use two regexp pieces that match non-colons. (The fields are delimited by colons; colons never appear in the fields themselves.) Remember each match, and swap them (p.78). *) Use a regular expression substitution in VIM to swap the first two adjacent numeric fields on every line, so that :9:13: becomes :13:9:, on every line. Make sure you only swap fields that contain all-digits, not mixtures of letters and digits. *) Use a regular expression substitution in VIM to remove all blanks that appear between letters (upper or lower case). Do not touch blanks that have non-letters on either side. (You will need to save the characters matched on each side.) *) (Tricky.) Use a regular expression substitution in VIM to swap the first field in the file with the last numeric field in the file, on every line. (Hints: Save the first field, the text between fields, and the last field in three separate registers using \( and \).) *) (Tricky.) Use a regular expression substitution in VIM to swap the first field in the file with the first numeric field in the file, on every line. (Hints: [see previous hint] The text between the two fields must not contain any digits.)