Search for Text with `grep`

Cameron Nokes
InstructorCameron Nokes

Share this video with your friends

Send Tweet
Published 4 years ago
Updated 3 years ago

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.

Lorenzo Gamboa
Lorenzo Gamboa
~ 3 years ago

This lessons seems to break at the beginning of the video.

Renato Nouman
Renato Nouman
~ 2 years ago

I am trying to grep a variable which is the output of a find command. I'm getting permission denied from the grep, but if I pass the same file directly it works. How could I solve that?

var=`find . -path ./node_modules -prune -o -path ./public -prune -o -type f -name "*.scss" -print`
grep "string" `$var`

Any tips?

~ 6 months ago

Strange, the grep command is only showing me 1 or 2 results when I invoke it on a keyword list with tons of results. My Keyword text file has hundreds of results containing what I invoke grep to find in it though. How could that be?