Contenues dansTrouver plus de documentationRessources d'assistance comprises | Télécharger cet ouvrage au format PDF (546 Ko)
Chapter 4 Searching FilesThis chapter describes how to search directories and files for keywords and strings using the SunOS command grep. 4.1 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:
where 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 may include "white space," 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 may 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." 4.1.1 grep as a Filtergrep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. 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 ending in ".ps" that were created in the month of May:
The first part of this command line,
produces a list of files:
The second part,
pipes that list through grep, looking for the pattern May:
4.1.2 grep with Multi-Word StringsTo find a pattern that is more than one word long, enclose the string with single or double quotation marks:
grep 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:
4.1.3 Searching for Lines without a Certain StringTo search for all the lines of a file that don't contain a certain string, use the -voption to grep. The following example shows how to find all of the lines in the user medici's home directory files that don't contain the letter e:
4.1.4 More on grepYou can also use the grep command to search for targets defined as patterns 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 and need to be quoted or escaped. Whenever you use a grep regular expression at the command prompt, surround it with quotes, or escape metacharacters (such as & ! . * $ ? and \) with a backslash (\).
4.1.5 Searching for MetacharactersSuppose you want to find lines in the text that have a dollar sign ($) in them. Preceding the dollar sign in the regular expression with a backslash (\) tells grep to ignore (escape) its special meaning. This is true for the other metacharacters (& ! . * ? and \ itself) as well. For example, the expression
matches lines starting with a period, and is especially useful when searching for nroff or troff formatting requests (which begin with a period). The following table, Table 4-1, provides a list of the more commonly used search pattern elements you can use with grep. Table 4-1 grep Search Pattern Elements
Note that these search characters can also be used in vi text editor searches. 4.1.6 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, you would type the following to use grep to search all files for the phrase "dang it, boys":
Single quotation marks (') can also be used to group multiword phrases into single units. Single quotation marks also make sure that certain characters, such as $, are interpreted literally. (The history metacharacter ! is always interpreted as such, even inside quotation marks, unless you escape it with a backslash.) In any case, it is a good idea to escape characters such as & ! $ ? . ; and \ when you want them taken as ordinary typographical characters. For example, if you type:
you will see all the lines in list. However, if you type:
you will see only those lines with the "$" character in them. For more information on the grep(1) command, refer to the man Pages(1): User Commands. |
|||||||||||||||||||||||||||||||||||||||||