CIT052 Index > Notes 2 > Answers to grep questions

Notes 2

Answers to grep questions

Given the following lines, which line or lines will each pattern find? Some patterns may not match any of the lines.

  1. I found a bug in the tenth line of my script.
  2. A fairly big southern state is abbreviated as TN.
  3. The teenager's baggage is in your room.
  4. This song's for you.
  5. Arms, legs, and hands.

Here are the patterns:

\([a-z]\)\1

The \( and \) “tag” the letter. The \1 means “match whatever the tagged letter was.

Effectively, this pattern looks for two of the same lowercase letter in a row. It matches the bb in abbreviated from line B, and the ee and gg in line C.

f[^aei]r

This matches the letter f, followed by any letter that is not an a, e, or i, followed by the letter r. The only line that matches is line D, with the word for. It does not match fairly in line B, because the f is followed by two vowels before the r, and a set of characters in brackets matches exactly one occurrence of a character in that set.

h.l

This matches the letter h followed by any character at all, followed by the letter l. This matches line A, which has the h at the end of tenth, a space (which counts as any character at all, followed by the l of line.

te*n

Match a t, zero or more occurrrences of e, followed by n. This matches ten in line A and teen in line C. It does not match TN in line B, because the matching is case sensitive.

ro*m

Match an r, zero or more of the letter o, and the letter m. This matches room in line C and the rm of arms in line E, which has zero occurrences of the letter o.

b[aeiou]g\>

Match a b, one of the vowels a, e, i, o, or u, and a g that is at the end of a word (\>). This matches bug in line A and big in line B, but not baggage in line C, because the g isn’t at the end of a word.

s[aeiou]*[s-z]

Matches an s followed by zero or more vowels, followed by one of the letters s through z. This matches the sou of southern in line B. It does not match the son of song's in line C, because the letter n is not in the range [s-z].

[ATGI]\{2,3\}

None of the lines has two or three of the characters A, T, G, or I in a row.