O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.3. Class names

Grep also allows the use of international character pattern matching or class name form of pattern matching.

Table 8.1. Class names and their regular expression equivalent
Class Regular expression equivalent
[[:upper:]] [A-Z]
[[:lower:]] [a-z]
[[:digit:]] [0-9]
[[:alnum:]] [0-9a-zA-Z]
[[:space:]] same as a space or a tab
[[:alpha:]] [a-zA-Z]

Let’s use a couple of examples on our file to see how these work. We want to extract all the product codes that start with a ‘5’ and have at least two upper case letters following them. This will do it:

						$ grep '5[[:upper:]][[:upper:]]' data.f 
483      Sept    5AP1996  USP     65.00   LVX2C  189 
483      may     5PA1998  USP     37.00   KVM9D  644
					

If we wanted to get all product codes that ended a ‘P’ or ‘D’ we could do ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required