Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Search for Text with `grep`

    Cameron NokesCameron Nokes

    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.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: 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.

    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.

    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.

    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.

    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.

    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.

    Grep has tons of flags. Look at main grep to view all the options.