Contained WithinFind More DocumentationFeatured Support Resources | Descargar este libro en PDF (736 KB)
Chapter 4 Searching FilesThis chapter describes how to search directories and files for keywords and strings by using the grep command. Searching for Patterns With grepTo search for a particular character string in a file, use the grep command. The basic syntax of the grep command is:
In this example, string is the word or phrase you want to find, and file is the file to be searched. Note – A string is one or more characters. A single letter is a string, as is a word or a sentence. Strings can include blank spaces, punctuation, and invisible (control) characters. For example, to find Edgar Allan Poe's telephone extension, type grep, all or part of his name, and the file containing the information:
Note that more than one line might match the pattern you give.
grep is case sensitive; that is, you must match the pattern with respect to uppercase and lowercase letters:
Note that grep failed in the first try because none of the entries began with a lowercase a. grep as a FilterYou can use the grep command as a filter with other commands, enabling you to filter out unnecessary information from the command output. To use grep as a filter, you must pipe the output of the command through grep. The symbol for pipe is “|”. The following example displays files that end in “.ps” and were created in the month of September.
The first part of this command line produces a list of files ending in .ps.
The second part of the command line pipes that list through grep, looking for the pattern Sep.
The search provides the following results.
grep With Multiword StringsTo find a pattern that is more than one word long, enclose the string with single or double quotation marks.
The grep command can search for a string in groups of files. When it finds a pattern that matches in more than one file, it prints the name of the file, followed by a colon, then the line matching the pattern.
Searching for Lines Without a Certain StringTo search for all the lines of a file that do not contain a certain string, use the -v option to grep. The following example shows how to search through all the files in the current directory for lines that do not contain the letter e.
Using Regular Expressions With grepYou can also use the grep command to search for targets that are defined as patterns by using regular expressions. Regular expressions consist of letters and numbers, in addition to characters with special meaning to grep. These special characters, called metacharacters, also have special meaning to the system. When you use regular expressions with the grep command, you need to tell your system to ignore the special meaning of these metacharacters by escaping them. When you use a grep regular expression at the command prompt, surround the regular expression with quotes. Escape metacharacters (such as & ! . * $ ? and \) with a backslash (\). See Searching for Metacharacters for more information on escaping metacharacters.
Searching for MetacharactersTo use the grep command to search for metacharacters such as & ! . * ? and \, precede the metacharacter with a backslash (\). The backslash tells grep to ignore (escape) the metacharacter. For example, the following expression matches lines that start with a period, and is useful when searching for nroff or troff formatting requests (which begin with a period).
Table 4–1 lists common search pattern elements you can use with grep. Table 4–1 grep Search Pattern Elements
Note that you can also use these search characters in vi text editor searches. Single or Double Quotes on Command LinesAs shown earlier, you use quotation marks to surround text that you want to be interpreted as one word. For example, type the following to use grep to search all files for the phrase “dang it, boys”:
You can also use single quotation marks (') to group multiword phrases into single units. Single quotation marks also make sure that certain that the system interprets metacharacters, such as $, literally. Note – The history metacharacter ! is always interpreted as a metacharacter, even inside quotation marks, unless you escape it with a backslash. Escape characters such as & ! $ ? . ; and \ when you want grep to interpret these characters as ordinary typographical characters. For example, if you type the following command, the system displays all the lines in the file list.
However, if you type the following command, the system displays only those lines that contain the “$” character.
For more information on the grep(1) command, refer to the man Pages(1): User Commands. |
|||||||||||||||||||||||||||||||||||||||||