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.2. grep and regular expressions

Using regular expressions we can now apply some rules to our pattern matching and be more selective about the information we want extracted. When we are using regular expressions it’s always a good idea to use single quotes, as this stops any special pattern you use in grep from being interpreted in strange ways by the command shell.

8.2.1. Pattern ranges

Suppose we wanted to get all the city locations that were either a 483 or 484 code. From the last chapter we know by using the [ ] brackets that we can specify a range of characters; here we specify that this will start with 48 and end with 3 or 4, thus extracting 483 or 484.

							$ grep '48[34]' data.f 483 Sept 5AP1996 USP 65.00 LVX2C 189 484 nov 7PL1996 CAD ...

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