Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the previous movie we learned to use grep to search by using a simple text string as our search expression. Grep becomes even more powerful once we learn to use regular expressions. Now there is a lot to learn about regular expressions. We can have an entire course on them, and we won't too in-depth here, but in this movie we will cover the most essential concepts so that you can apply them and use them with grep. To begin with notice that in the Terminal I'm inside my user directory and I'll just change it to my unix_files directory. And the file that we're going to start by working with is the same fruit.txt file that we're working with in the last movie.
It's just a simple list of fruit that we can search inside of and see how regular expressions work. To begin with we saw that we can grep for apple inside fruit.txt. First thing I want to tell you about regular expressions is that it's a good idea to put quotes around it. Now they are not strictly required, you saw it worked just fine without them, but it's a good idea. And the reason why is because when we start working with regular expressions we're going to be using some special regular expression symbols. And some of those regular expression symbols also have a meaning to the Unix command line and by putting it in quotes we're helping the Unix command line to see ah, this is the expression.
The symbols that I see in there they are not for me to interpret and for me to do things with. They're meant to be part of the regular expression. So the single quotes are good habit to be in. Now the string that we've been searching for, apple, that is a regular expression. It's the simplest regular expressions that you can have, because it matches only the literal characters apple. Instead what we want to do is be able to provide some variation to that. So for example, we can replace those p's with periods. The period is a wild card character for regular expressions.
It says any character could match here. So now we're not just looking for apple we're looking for a followed by any character, followed by any other character, followed by l and e. It has to be 2 character. It can't be 1, but 2 characters, but we don't care what those characters are. So now it matches not just apple, but it matches ankle. Now it's not that useless in this case, because it really just does match apple just the same, but let's try another example. Let's do grep that file for .a.a.a inside fruit. Do you know what it's going to return? Now we get back both banana and papaya.
The reason why is because each of them matches the pattern that we've described. Any character followed by an a, any character followed by an a, and then any character followed by an a, and both of them match this pattern. And that's really what we're doing with regular expressions. We're defining patterns that we want to use for searching. So you can think of that. You can think of regular expressions as ways of defining patterns that we want to search for. Let's try another one let's just grep the file for e and a together, all right so, anytime that we have ea together. So it returned pear, peach, pineapple, and pear.
Now if we wanted to just get pear and peach, we can just put a p at the beginning, right? And that would rule out pineapple. That would no longer match our pattern, but if we wanted to get peach and pineapple and omit pear how we go about doing that? Well, we can say for example that we want the third character after the e and a to match either c or p, but not the r. Now you can see it returns peach and it returns pineapple, but it didn't return pear. We're going to talk more about the square brackets, but I want you to just understand the concept that what we're doing here is we're defining a pattern that we wanted to match and both eac and eap match our pattern.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98749 Viewers
61 Video lessons · 86078 Viewers
71 Video lessons · 69925 Viewers
56 Video lessons · 102191 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.