Grep Regex Tool For Mac

I wanted a free grep tool for Windows that allowed you to right click on a folder and do a regex search of every file - without any nag screen. The following is a quick solution based on the findstr mentioned in a previous post. As you can see, grep is a very powerful tool. It can be used to quickly search files and to filter output on the command line. It does have a couple limitations, though. First, it is no speed demon.

It's fast, it's powerful, and its very name suggests that it does something technical: grep. With this workhorse of the command line, you can quickly find text hidden in your files. Understanding grep is the first step in joining the guild of command-line wizards.

Why Not Use the Finder?

It's easy to find files with the Finder when you know their names. But the grep command is a time-saver when you're trying to find what's inside files. You can use grep easily from the command line to search for specific text, and you'll get results in seconds. (You can also use grep within certain text editors.)

The Finder offers a similar function: the Find By Content search. (Press Command-F in the Finder, select Content in the Search For Items Whose pop-up menu, and enter a search string in the text field.) But the Finder searches only inside files it has indexed, and it ignores hidden system files unless you expressly choose to search for visible and invisible files and add your System folder to the search.

The Finder also lacks grep's flexibility: while it's good for searching for a specific word (for example, Walden ), it becomes less useful when you want to find a longer string. Search for Walden Pond, and it returns all files that contain either Walden or Pond.

Using grep also gives you access to regular expressions. These combinations of letters, numbers, wild cards, and other special characters let you find much more than mere words. You can search for just about any string of characters: IP addresses in log files; phone numbers in contact lists; or specific strings containing a series of numbers, letters, or other characters. Using regular expressions, you're limited only by your imagination.

Get a Grip on Grep

Grep Regex Tool For Mac

The grep command looks inside one or several files for the string, or text, you specify. Its syntax is: grep options search_string file....

At its most basic, you tell grep what to look for and where: grep AppleTalk /etc/services, for example. Here, you tell grep to look for AppleTalk in the services file located in the /etc directory. (This useful file contains a list of network port numbers for commonly used services.) The command displays each line that contains your search string:

And so on. You can use the familiar asterisk (*) wild card to have grep search a group of files in the current working directory, as in grep Walden *. This command searches all the files in the current directory for the word Walden, returning the following:

And so on. As the above example shows, the search returns several matches. The first, Walden.doc, is a Word file. The grep command calls such proprietary file types binary files. It can't display the contents of binary files, but it can search inside them and tell you if something matches. The next examples come from plain-text files, for which grep can display the results. You see the file name, followed by a match of the search string in its context.

You can search for any multiword text string by enclosing the string in single quotes. For example, if you want to search for the phrase Walden Pond,you'd type grep 'Walden Pond' *.

Note that grep doesn't like Macintosh line breaks. It returns lines containing the search string, but it doesn't see Mac line breaks as anything other than characters. In such a case, the “line” it returns is the entire file; this can dump a lot of text into your Terminal window.

In the previous example, grep ran in a specific folder, checking all the files it contained. What if you want to run grep on a folder and its subfolders, or you want grep to look for the string regardless of case? You need to add options. For example, to search for Waldenanywhere in a folder or its subfolders, use the-r(recursive) option: grep -r Walden ~/Documents/*.

Fine-Tune Your Searches

To Find…Use This OptionExample
Text in subfolders-rgrep -r Walden ~/Documents/*
Finds Walden in any file in any subfolder of ~/Documents.
Whole words only-wgrep -w live
Finds only live ; does not find liver , lives , lived , and so on.
Case-insensitive text-igrep -i pond
Finds pond , POND , or Pond .
File names only-lgrep -l Walden
Finds files containing Walden , but returns only a list of file names.
Number of occurrences only-cgrep -c Walden
Returns the names of files containing Walden and the number of hits in each file.

The grep command has several options that let you fine-tune the way you search for text, as well as the kind of results grep returns. Get started with the helpful options listed here. (Note that you can also combine options—for instance, grep -rl Walden searches subfolders and returns only a list of files containing the word Walden .)

Search for Multiple Strings

Using the pipe (|), a Unix redirection operator, you can tell grep to search for more than one string. Say you want to find files containing both Walden and Pondon the same line. You'd use this command: grep Walden * | grep Pond. The first part of the command looks for the word Walden in any files in the current directory, and the second runs another grep command on the results of the first command. Terminal displays only the final results of the two commands combined.

You could string together many grep commands, like this:

. This command looks in a special dictionary file for words containing the lowercase letter a. It then looks for words containing e in the results, and so on, finally returning only those words that contain all five vowels.

This function of grep is most useful when you're searching for specific strings in output from other commands. In this way you can whittle down long and complex output. For example, here's a common way to find the process ID of a program that's stuck so you can force-quit it from the command line: type ps -ax | grep Finder.

This command first gets a list of all processes running on your Mac, and then sifts through this list looking for lines containing the word Finder. For example, your results might be as follows:

The process ID is the first number on each line; here, the Finder is 390. (You'll notice the command also returns itself; since the word Finderis in the grep command, that gets listed as well.) So if the Finder is stuck, this gives you the information you need to force-quit it. Now you would type the command (where the process ID is the final argument) kill -9 390.

Add Regular Expressions to the Mix

While you have seen some of the power of the grep command, you can go much further using regular expressions, special combinations of characters that act as wild cards. Here are a few examples.

If you're not sure how to spell the word separate, for example (is that an a or an e?), run this command to check the special dictionary file hidden in your Mac's entrails:

You'll get back a list of words that includes separate, separately, separately, separateness, and separates.

Note the two special characters in the command: the caret (^) and the dot (.). The caret tells grep to search for the string at the beginning of a line, so the results don't include words like inseparate. The dot matches any character except a new line.

What if you want to find all the phone numbers in a specific file? Try this command, which will find phone numbers in the 555-1234 format:

.

Each of the[0-9] wild cards matches any character in the range specified in brackets. You can use ranges such as[1-3] to limit your search to specific strings. This works for letters, too:[a-n] matches any lowercase character from a to n.You can build your own range with sets of characters—for example, [aeiou] will match only vowels. You can learn more about regular expressions by typingman grep in Terminal, or by consulting Jeffrey Friedl's excellent book Mastering Regular Expressions, second edition (O'Reilly, 2002). With a bit of practice, you'll be using grep and regular expressions to find anything you want.

Kirk McElhearn is the author of The Mac OS X Command Line: Unix Under the Hood (Sybex, 2004). His blog, Kirkville, has articles and tips on using the command line with Mac OS X. ]

Note: When you purchase something after clicking links in our articles, we may earn a small commission. Read our affiliate link policy for more details.

As every Linux user surely knows, grep is a reliable command-line tool for in-depth file searching. Still, many beginners avoid it because they dislike the terminal. The apps presented in this article aren’t exactly alternatives to grep because in some usage scenarios grep is truly irreplaceable. Instead, let’s call them visual upgrades for grep because they extend grep’s functionality and wrap it in a full-fledged graphical interface.

1. Regexxer

Regexxer is a practical file search tool that lets you edit files directly from its interface. You can search for files and folders by name and look inside text-based files (including HTML and XML files). The left side of the window lets you select the target folder and pattern (put * for all files or *.txt just for text files). Regexxer can perform recursive search in subfolders of any selected folder and include hidden files in the results.

The right side of the window lets you perform “search and replace” on a selected file. Here you can replace only one instance of a found phrase or all of them automatically. You can also replace the selected phrase in all found files which is useful for batch-editing.

2. Searchmonkey

Back in the day Searchmonkey was very popular. At some point, the development of the Linux version ceased, and now the website offers new downloads for Windows only. Still, the old version can be installed from the repositories of nearly every Linux distribution. Perhaps surprisingly, it works great and it’s really fast. You can use Searchmonkey to find files and folders by name, or to look through their contents and search for phrases using regular expressions.

Searchmonkey helps you build complex queries with the File Expression Wizard (activated by clicking the Expression Builder button) and an option called Test Regular Expression (in the Extras menu). It can search for files recursively, and you can set the search depth (how many subfolders it should look into) and filter files by size and date. In the “Options” tab, you can limit the number of files in the results and choose how many lines of context you want to see.

3. DocFetcher

Instead of directly search your filesystem, DocFetcher will ask you to build an index and then look for your queries only in indexed files. It offers a portable version (just unpack it and run the .sh file from the terminal) for both 32- and 64-bit systems. To build an index, right-click in the “Search Scope” area on the left.

You can add folders to the index, pause index creation and continue it later, index archive files (ZIP, TAR) as folders, and exclude selected files from the index with the help of regular expressions.

DocFetcher has a built-in HTML renderer which lets you preview HTML files complete with formatting and images. It offers a privacy-conscious option to remove search history and lets you search for and within files using wildcards, Boolean operators, fuzzy search (finds similar words), proximity search (how far the words should be from each other in text), and more. DocFetcher supports an impressive number of formats, including Microsoft and Libre Office files (DOC, DOCX, ODT, OTP …), PDF and EPUB, HTML and XML, Outlook PST email files, and audio and image metadata.

4. Regain

Regain is a search engine for your desktop; something like Google, but for your files and folders. It’s written in Java, so it works on Linux, OS X and Windows, provided that you have Java properly installed and configured. The installation file is available on the project website, and you can simply extract it into a folder, open that folder in the terminal and run java -jar regain.jar to start the application. (The file “regain.jar” has to be executable). Regain will run in your default web browser.

To search for your files and folders, Regain must first crawl your system and build a search index. In the “Preferences” form, you add the folders which you want indexed. If you don’t want to include particular files in the index, blacklist them in the “CrawlerConfiguration.xml” file. Once you start using Regain, it will search the index instead of scanning the entire hard drive. This saves system resources and makes searching faster.

Snippet Tool For Mac

5. PDFgrep

Grep Regex Match

Of all the tools on this list, PDFgrep is the most similar to original grep, but it’s also “the odd one out”, because it’s a command-line tool. Several distributions offer PDFgrep in their repositories, but the newest version (currently 1.3.2) has to be compiled.

While grep outputs the line number in which the search string appears, PDFgrep will show you the page number instead, which is more useful for PDF files as we tend to read them like books, not analyze them line by line. PDFgrep works only on PDF files. They have to be either converted from text or OCR-ed, not just scanned images.

To search for a word in a PDF file, type:

To ignore the case, use the option -i:

This will find “Word”, “word”, “WORD” and other possible combinations. If you’re looking for a phrase, enclose it in quotation marks. Some useful options are:

  • -n: outputs the page number for every match
  • -c: prints only the number of matches in a file
  • -p shows the number of matches per page
  • -C NUMBER: prints the selected number of characters around every match for context. Instead of a number, you can write “line” and PDFgrep will print the entire line.

PDFgrep can recursively search in all subfolders of an active folder and look through multiple PDF files. It also supports regular expressions, and options can be combined:

This would print the page number and the filename for each match (because of the -H option).

Which Linux tools and commands do you use to find files? Share your favorites in the comments below.

Image credit: Featured image source, Teaser image source

Ebooks