Using [] and ^

In this section, we will learn more about REs, particularly when using and negating ranges using the [] and ^ characters.

You've noticed that the robtest.txt file you created and have been using has the years 1899 through 1999 inside of it. We will use grep again in conjunction with REs to match the years that only fall in the 2000 range. To do this, you can specify a range using REs as follows: [<starting point>-<ending point>]. The starting and ending points can be numbers or ranges within the alphabet.

For example, type the following:

					> grep "1[8-9]9[0-9]*" robtest.txt
					1999
					1899
				

This example lets Unix search robtest.txt for anything that is in the 1[8-9][9-0] range, which is only 1999 and 1899. It couldn't be anything from ...

Get SAMS Teach Yourself Unix in 10 Minutes now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.