Using . and *

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.

Okay, Don't Set Me Off Now! There are more ways to use grep and REs. This chapter can only cover so much, so it's my intent to interest you and then you can look on your own.

Use to set off a special character. Some characters are used by the shell, so they must be escaped by using . You might want to use this in front of characters that might be special characters as well. In most cases, it doesn't hurt to use if you aren't sure. For example, the shell usually expects you to put double quotes (") around strings with spaces in them. It uses the double quotes to group the words in the string. If you need to search through your file for lines containing double quotes, you cannot grep for "; instead, use the following command:

> grep " robtest.txt
"aka Unix junkie"

Using the in front of the double quotes tells the shell to not attempt to interpret the double quotes normally as a surrounding character, but to instead simply pass it to the grep command for processing.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.191.94