

Although these sources are a great reference, we will be showing the grep command by example. The man page is very extensive, so is the online help documentation. Why learn the grep command and regular expressions?Īs with every tool, it is often easy to start using it, but hard to really master it. It is common to use it for checking configuration files and searching through log files. We want to leverage the full potential of the grep command, as it can be used in many work-related or personal related activities. But with all information scattered, most people don’t take the time to really learn the most basic commands. One of the reasons to create this blog post is that there are a lot of examples available for the grep command. Searching inside compressed data (avoid using gunzip!).Find lines ending with a specific string.Find lines starting with a specific string.Recursive search through directories and files.Why learn the grep command and regular expressions?.
Macos grep options manual#
Still, even peons like me can use it to do simple line-searches! If you’re braver than I and would like to delve into it more deeply, just open Terminal and type man grep at the prompt to read more of its manual page. The grep command is actually incredibly powerful, and some of its functionality is admittedly way over my head. So for example, if you wanted to find lines containing “scary sewer clown,” you’d type this:
Macos grep options how to#
You have to know a bit about how to use file paths to really understand what you’re doing here, but in plain English, my command above says “find lines matching ‘test’ in the ‘Grep.txt’ document on Melissa’s desktop, then put the resulting data into a file called ‘testfile.txt,’ also on her desktop.” Be careful, though, that you aren’t pointing Terminal to an existing file! If you already have a “testfile.txt” on your desktop, this command will overwrite it.įinally, one more detail-if your search term contains any special characters (such as spaces, say), you’ll have to put a backslash in front of them to make Terminal interpret them correctly. Or if you’re more fancy, you could use a right-angle bracket (“>”) to “pipe” the info out into a new file by adding on to your command, like this: When you get to this point with your own grepping, which is probably a word I just made up, you can copy and paste the resulting data out of Terminal. If I then press Return on my keyboard, the Terminal window will fill up with the lines that match my search! Neat! Once I do that, the program’ll fill in the path to the file for me, easy-peasy.


An easy way I can do this is by making sure to type a space after my search term, and then I’ll drag and drop the file I want to search onto the Terminal window, like so:

So I’ve entered the grep command followed by my search term-“test”-and now I just have to tell Terminal which file to run things on. What I’ll do first is open Terminal, of course-it lives in my Applications > Utilities folder-and after it gives me a prompt to start (ending with a dollar sign), I’ll type this: If what I need to do is take all of the lines containing “test” and separate them from the lines containing everything else, that’s really simple for grep. Boy, I am just stupidly bad at coming up with random words. Here’s how it works: Let’s pretend this text file of mine has many hundreds of lines of data that I need to paw through. What this means in more simple terms is that you can use grep to pull lines that contain search terms out of a text file.
