In this lesson, we’ll use grep
to find text patterns. We’ll also go over some of the flags that grep
has that can be combined together to make it more powerful and easier to read.
Instructor: [00:01] I've pulled MPMs through a repository here locally. Let's say I want to find all uses of mpm.config.get. I want to look for that in [inaudible] mpm.js file. Each line is a match that was found on the file. These lines aren't necessarily next to each other in the file or anything. These might be totally different lines all over the file.
[00:22] I can tell it to search multiple files as well. That was just one file. If I wanted to, say, search every JavaScript file in the [inaudible] parent folder, I could do that. That will output a ton of files. The output's a little different when you're searching multiple files. You can see it has the file name here and then, there's a match. It's same file name there, and then there's the match there later.
[00:47] That output is a little difficult to read. We can add more flags to grep to change the output and make it easier to read. Let's run this first one again.
[00:55] If we add the color flag, that's the same thing, but it colorizes the match. Another useful one is the end flag. This will output the line number it's found on, which can be useful if you want to then go open the file in the text editor and jump right to the line. Another one that's useful is the context flag. This shows context around each match.
[01:16] If I pass 1 to it, you can see it shows the match here and then, it shows the single line around the match. We can, of course, change that to whatever we want if we want more context. Also, notice in this display that it separates each matching block. Grep also supports regex. If we do grep, it'll do color, line numbers, and it'll do e for regex. Then, we'll do the same thing again.
[01:44] There's a config.get and a config.sent method. We'll do that. We'll use a regex to be tricky and look for both of them. We'll search the mpm.js file. We can see that that is matching both of those methods there.
[01:58] Grep has tons of flags. Look at main grep to view all the options.