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.
By default, Windows Search will use a plain text filter to search the contents of those types of files, since another app is not associated. After the index is rebuilt, searching for text inside one of the new file types should now show results.
Get a Grip on Grep
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 Option | Example |
---|---|---|
Text in subfolders | -r | grep -r Walden ~/Documents/* Finds Walden in any file in any subfolder of ~/Documents. |
Whole words only | -w | grep -w live Finds only live ; does not find liver , lives , lived , and so on. |
Case-insensitive text | -i | grep -i pond Finds pond , POND , or Pond . |
File names only | -l | grep -l Walden Finds files containing Walden , but returns only a list of file names. |
Number of occurrences only | -c | grep -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. ]
Go to Home > Replace or press Ctrl+H.
Enter the word or phrase you want to locate in the Find box.
Enter your new text in the Replace box.
Select Find Next until you come to the word you want to update.
Choose Replace. To update all instances at once, choose Replace All.
To specify only upper or lowercase in your search, select More > Match case.
For other options, see Find and replace text
Find and replace basic text
In the upper-right corner of the document, in the search box , type the word or phrase that you want to find, and Word will highlight all instances of the word or phrase throughout the document.
To replace found text:
Select the magnifying glass, and then select Replace.
In the Replace With box, type the replacement text.
Select Replace All or Replace.
Tips:
You can also open the basic Find and Replace pane with the keyboard shortcut CONTROL + H.
When you replace text, it's a good idea to select Replace instead of Replace All. That way you can review each item before replacing it.
Find text with specific formatting
You can find text with special formatting, such as bold or highlight, by using the Format menu.
Select View > Navigation Pane.
In the Navigation Pane, select the magnifying glass.
Select Settings , and then select Advanced Find & Replace.
Notes:
Select the arrow at the bottom of the Find and Replace dialog box to show all options.
On the Format menu, select the option that you want.
If a second dialog box opens, select the options that you want, and then select OK.
In the Find and Replace dialog box, select Find Next or Find All.
How To Print Only Selected Text On A Mac
Find text with specific formatting and replace the formatting
You can find and replace text with special formatting, such as bold or highlight, by using the Format menu.
Select View > Navigation Pane.
In the Navigation Pane, select the magnifying glass.
Select Settings , and then select Advanced Find & Replace.
At the top of the dialog box, select Replace.
Notes:
Select the arrow at the bottom of the Find and Replace dialog box to show all options.
On the Find what box, type the text that you want to find.
On the Format menu, select the formatting that you want to find.
If a second dialog box opens, select the options that you want, and then select OK.
Select in the box next to Replace with.
On the Format menu, select the replacement formatting. If a second dialog box appears, select the formats that you want, and then select OK.
Select Replace, Replace All, or Find Next.
Find paragraph marks, page breaks, or other special characters
Select View > Navigation Pane.
In the Navigation Pane, select the magnifying glass.
Select Settings , and then select Advanced Find & Replace.
Notes:
Select the arrow at the bottom of the Find and Replace dialog box to show all options.
On the Special menu, select the special character that you want to find.
Select Find Next.
How Do You Copy Text On A Mac
Find and replace paragraph marks, page breaks, or other special characters
Select View > Navigation Pane.
In the Navigation Pane, select the magnifying glass.
Select Settings , and then select Advanced Find & Replace.
Notes:
Select the arrow at the bottom of the Find and Replace dialog box to show all options.
At the top of the Find and Replace dialog box, select Replace and then select in the Find What box, but don't type anything there. Later, when you select a special character, Word will automatically put the character code in the box for you.
Note: Select the arrow at the bottom of the Find and Replace dialog box to show all options.
On the Special menu, select the special character that you want to find.
Select in the Replace with box.
On the Special menu, select the special character that you want to use as a replacement.
Select Replace or Find Next.
Use wildcard characters to find and replace text
Select View > Navigation Pane.
In the Navigation Pane, select the magnifying glass.
Select Settings , and then select Advanced Find & Replace.
Select the Use wildcards check box.
If you don't see the Use wildcards check box, select .
Select the Special menu, select a wildcard character, and then type any additional text in the Find what box.
Select Find Next.
Tips:
To cancel a search in progress, press + PERIOD.
You can also enter a wildcard character directly in the Find what box instead of selecting an item from the Special pop-up menu.
To search for a character that's defined as a wildcard character, type a backslash () before the character. For example, type ? to find a question mark.
You can use parentheses to group the wildcard characters and text and to indicate the order of evaluation. For example, search for <(pre)*(ed)> to find 'presorted' and 'prevented.'
You can search for an expression and use the n wildcard character to replace the search string with the rearranged expression. For example, type (Newman) (Belinda) in the Find what box and 2 1 in the Replace with box. Word will find 'Newman Belinda' and replace it with 'Belinda Newman.'
To replace found text:
Select the Replace tab, and then select the Replace with box.
Select Special, select a wildcard character, and then type any additional text in the Replace with box.
Select Replace All, Replace, or Find Next.
Tip: When you replace text, it's a good idea to select Replace instead of Replace All. That way you can confirm each replacement to make sure that it's correct.
Wildcard characters you can use
You can refine a search by using any of the following wildcard characters.
To find | Use this | For example |
---|---|---|
Any single character | ? | s?t finds 'sat' and 'set.' |
Any string of characters | * | s*d finds 'sad' and 'started.' |
One of the specified characters | [ ] | w[io]n finds 'win' and 'won.' |
Any single character in this range | [-] | [r-t]ight finds 'right' and 'sight' and 'tight.' Ranges must be in ascending order. |
Any single character except the characters inside the brackets | [!] | m[!a]st finds 'mist' and 'most' but not 'mast.' |
Any single character except characters in the range inside the brackets | [!x-z] | t[!a-m]ck finds 'tock' and 'tuck' but not 'tack' or 'tick.' Ranges must be in ascending order. |
Exactly n occurrences of a character or expression | { n} | fe{2}d finds 'feed' but not 'fed.' |
At least n occurrences of a character or expression | { n,} | fe{1,}d finds 'fed' and 'feed.' |
A range of occurrences of a character or expression | { n, n} | 10{1,3} finds '10,' '100,' and '1000.' |
One or more occurrences of a character or expression | @ | lo@t finds 'lot' and 'loot.' |
The beginning of a word | < | <(inter) finds 'interesting' and 'intercept' but not 'splintered.' |
The end of a word | > | (in)> finds 'in' and 'within,' but not 'interesting.' |
Search Files For Text On Mac
Word for the web lets you find and replace basic text. You can match case or fine whole words only. For more varied options, open your document in Word for the desktop.