Free Hosting Online for WorkStations

< Previous | Contents | Next >

Traditional Character Ranges

If we wanted to construct a regular expression that would find every file in our lists be - ginning with an uppercase letter, we could do this:



[me@linuxbox ~]$ grep -h '^[ABCDEFGHIJKLMNOPQRSTUVWXZY]' dirlist*.txt

[me@linuxbox ~]$ grep -h '^[ABCDEFGHIJKLMNOPQRSTUVWXZY]' dirlist*.txt


It’s just a matter of putting all 26 uppercase letters in a bracket expression. But the idea of all that typing is deeply troubling, so there is another way:



[me@linuxbox ~]$ grep -h '^[A-Z]' dirlist*.txt

MAKEDEV

ControlPanel GET

HEAD POST X X11

Xorg MAKEFLOPPIES

NetworkManager NetworkManagerDispatcher

[me@linuxbox ~]$ grep -h '^[A-Z]' dirlist*.txt

MAKEDEV

ControlPanel GET

HEAD POST X X11

Xorg MAKEFLOPPIES

NetworkManager NetworkManagerDispatcher


By using a three character range, we can abbreviate the 26 letters. Any range of charac -


ters can be expressed this way including multiple ranges, such as this expression that matches all filenames starting with letters and numbers:



[me@linuxbox ~]$ grep -h '^[A-Za-z0-9]' dirlist*.txt

[me@linuxbox ~]$ grep -h '^[A-Za-z0-9]' dirlist*.txt


In character ranges, we see that the dash character is treated specially, so how do we actu- ally include a dash character in a bracket expression? By making it the first character in the expression. Consider these two examples:



[me@linuxbox ~]$ grep -h '[A-Z]' dirlist*.txt

[me@linuxbox ~]$ grep -h '[A-Z]' dirlist*.txt


This will match every filename containing an uppercase letter. While:



[me@linuxbox ~]$ grep -h '[-AZ]' dirlist*.txt

[me@linuxbox ~]$ grep -h '[-AZ]' dirlist*.txt


will match every filename containing a dash, or an uppercase “A” or an uppercase “Z”.


Top OS Cloud Computing at OnWorks: