Let's look at building a new file to practice using regular expressions. In this example, we will use grep in conjunction with the . and * REs. Since REs will specify a method of matching, I will attempt to drive home the concept of REs with a search through a simple text file that you can create with the vi editor or emacs.
Make a file that has the current information within it:
Rob's Test File Rob Shimonski "aka Unix junkie" 2006 2005 2004 2003 2002 2001 2000 1999 1899
Once you have finished, save and name the file robtest.txt.
This file will serve as the data we will search to learn how to use REs. In this example, we will use the period (.), which can be used to match any character as a single unit, and the asterisk (*), which you can use to match any number of occurrences of a pattern or portion of a pattern. To make this concept easier to understand, let's look at an example of both when using REs with grep to search the robtest.txt file for specific information.
> grep "Shimon..." robtest.txt Rob Shimonski
In this example, we saw the use of grep, which was used against the robtest.txt file to search for my last name “Shimonski.” It was able to do so, even though I left the last three letters “ski” off, and intentionally put in three periods so that Unix could come back to me with what it found in the robtest.txt file as a match. This can be used in multiple ways, such as the following:
> grep "Shimon..i" robtest.txt Rob Shimonski > grep "Shi..n.ki" robtest.txt Rob Shimonski > grep "S..mo..ki" robtest.txt Rob Shimonski
As you can see, it really doesn't matter what you specify, you just need to specify what is a known exactly as shown in the file so that Unix can find it for you. Unix will attempt to find what it thinks you are looking for, so don't be surprised if you don't narrow your search down and you get thousands of answers from Unix. The period (.) is primarily used to narrow down your search. In cases where you don't really know, and don't mind the possibility of a long and timely search, you can use the asterisk (*).
If you were able to do the first example, then you are definitely able to handle this one because all you are doing is applying a slightly different concept here:
> grep "S.*i" robtest.txt Rob Shimonski
With last names like mine, using a wildcard is sometimes your only hope.
3.137.191.94