Combining Special Characters

# match lines that don't end with "."

===> grep '[^\.]$' fortunes

Words are the voice of heart

E Pluribus UNIX

Helping others ==> we all benefit

Disk crisis, please clean up!

Love the Sea? I dote upon it - from the beach

The future isn't what it used to be!

Three witches had three watches. Which witch was watching which watch?

You might have mail

The End

===> grep '^stu0[^2-6]a' /etc/passwd

stu01a:*:501:501:Student Account:/u/students/stu01a:/bin/ksh

stu07a:*:507:507:Student Account:/u/students/stu07a:/bin/ksh

stu08a:*:508:508:Student Account:/u/students/stu08a:/bin/ksh

stu09a:*:509:509:Student Account:/u/students/stu09a:/bin/ksh

Combining Special Characters

Previous slide Next slide Back to the first slide View Graphic Version

Notes:

The first example will print out those line which don’t end with the period (.) in the fortunes file. Notice the characters range within square braces:

Thus, lines 1, 11, 15, 16, 18, 24, 33, 34, and 36 satisfies the expression. When a period is placed in square braces, the special meaning is negated. However, my preference is to still include the backslash to eliminate any confusion for those who aren’t that familiar with UNIX regular expressions.

In the second example we want to match those records, in the /etc/passwd file, where the first character is a “s” character at the beginning of the line; the next (second) is a "t"; the third is an "u"; the fourth is a “0" (zero); the fifth character is anything that doesn’t match a digit ranging from "2" thru "6"; and the sixth character is an "a".