GREP, EGREP, FGREP(1,C) AIX Commands Reference GREP, EGREP, FGREP(1,C) ------------------------------------------------------------------------------- grep, egrep, fgrep PURPOSE Searches a file for a pattern. SYNTAX +-------------------+ +------------+ grep ---| 1 +----------+ |---| +----+ |---> +- -p -| |-+ +---| -s |---+ +- parsep -+ ^ | -v | | | | -i | | | | -w | | | | -q | | | +----+ | +--------+ +----------------+ +-- pattern ---+ +------------+ >---| one of |---| |---| |---| | +----+ | +- -e pattern -+ +--- file ---+ | | -c | | ^ | | +---| -l |---+ | +--------+ +-| +----+ |-+ | +----+ | +---| -b |---+ ^ | -n | | | +----+ | +--------+ +--------+ +----------------+ egrep ---| +----+ |---| one of |---> +-| -v |-+ | +----+ | ^| -s || | | -c | | || -h || | +---| -l |---+ | |+----+| +-| +----+ |-+ +------+ | +----+ | +---| -b |---+ ^ | -n | | | +----+ | +--------+ +---- pattern ----+ +------------+ >---+- -e pattern -+---| |---| +- -f stringfile -+ +--- file ---+ ^ | +--------+ ----------------- 1 No space allowed between these arguments. Processed August 5, 1991 GREP, EGREP, FGREP(1,C) 1 GREP, EGREP, FGREP(1,C) AIX Commands Reference GREP, EGREP, FGREP(1,C) +------------+ +----------------+ fgrep ---| +----+ |---| one of |---> +---| -v |---+ | +----+ | ^ | -x | | | | -c | | | | -i | | | +---| -l |---+ | | | -s | | +-| +----+ |-+ | | -h | | | +----+ | | +----+ | +---| -b |---+ +--------+ ^ | -n | | | +----+ | +--------+ +---- pattern ----+ +------------+ >---+- -e pattern -+---| |---| +- -f stringfile -+ +--- file ---+ ^ | +--------+ DESCRIPTION Commands of the grep family search input files (standard input, by default) for lines matching a pattern. Normally, these commands copy each line found to standard output. Three versions of the grep command permit you to express the matching pattern in varying levels of complexity. The versions are: grep Searches for patterns, which are limited regular expressions in the style of the ed command. The grep command uses a compact non-deterministic algorithm. egrep Searches for patterns which are full regular expressions as in the ed command, except for "\(" and "\)" and with the addition of the following rules: o A regular expression followed by a + (plus sign) matches one or more occurrences of the regular expression. o A regular expression followed by a "?" (question mark) matches 0 or 1 occurrences of the regular expression. o Two regular expressions separated by a | (vertical bar) or by a new-line character match strings that are matched by either expression. o A regular expression may be enclosed in () (parentheses) for grouping. A regular expression can be an internationalized regular expression for matching character classes, character collation sequences, and character ranges in any language the user wants to use. The syntax for the additional support is expressed within square brackets. Processed August 5, 1991 GREP, EGREP, FGREP(1,C) 2 GREP, EGREP, FGREP(1,C) AIX Commands Reference GREP, EGREP, FGREP(1,C) The constructs are described as follows: c The single character c, where c is not a special regular expression character (like "+" or "*"). [[:class:]] A character class expression. One of the following should be substituted for "class": alpha a letter upper an upper case letter lower a lower case letter digit a decimal digit xdigit a hexadecimal digit alnum an alphanumeric (letter or digit) space a character producing white space in displayed text punct a punctuation character print a printable character graph a character with a visible representation cntrl a control character For example, the regular expression [[:lower:]] represents the set of all lower case characters in the user's current locale. [[=c=]] An equivalence class. Any collation element defined as having the same relative order in the current collation sequence as c. For example, if A and a belong to the same equivalence class, then the two regular expressions [[=A=]b] and [[=a=]b] are both equivalent to [Aab]. [[.cc.]] A collating symbol. Multi-character collating elements must be represented as collating symbols to distinguish them from single-character collating elements. For example, in Spanish, the string "ch" is a valid two-character collating element, so the regular expression [[.ch.]] will be treated as an element matching the same string of characters in the input data file. However, when c and h appear together without the collating symbol, these characters will be treated as individual letters without a special meaning. If the string is not a valid collating element in the current collating sequence definition, the collating symbol will be treated as an invalid expression. [c-c] A character range. Any character collates within the character expression range c-c, where c can be a collating symbol, an equivalence class, or a single character. If the character "-" appears immediately after an opening square bracket (for example [-c]), or Processed August 5, 1991 GREP, EGREP, FGREP(1,C) 3 GREP, EGREP, FGREP(1,C) AIX Commands Reference GREP, EGREP, FGREP(1,C) immediately prior to a closing square bracket (for example [c-]), it has no special meaning. Within square brackets, a "." that is not part of a [[.cc.]] expression, a ":" that is not part of a [[:class:]] expression, or a "=" that is not part of a [[=c=]] expression, will match itself. The order of precedence of operators is [], then "* ? +", then concatenation, then | and the new-line character. The egrep command uses a deterministic algorithm that needs exponential space. fgrep Searches for patterns which are fixed strings. It searches for lines that contain one of the strings (lines are separated by new-line characters). All versions of the grep command display the name of the file containing the matched line if you specify more than one file name. Characters with special meaning to the shell ("$ * [ | ^ ( ) \"), must be quoted when they appear in patterns. When pattern is not simple string, you usually must enclose the entire pattern in single quotation marks. In an expression such as [a-z], the minus means "through" according to the current collating sequence. A collating sequence may define equivalence classes for use in character ranges. See the "Introduction to International Character Support" in Managing the AIX Operating System for more information on collating sequences and equivalence classes. The exit value of these commands is: 0 A match was found. 1 No match was found. 2 A syntax error was found or a file was inaccessible (even if matches were found). Notes: 1. The maximum line length is 512 characters; longer lines are broken into multiple lines of 512 or fewer characters (the grep command only). 2. Paragraphs (under the -p flag) are limited to a maximum length of 5000 characters. If you have selected a language (through the LANG environment variable) that supports multibyte characters, the character limits can be reduced by as much as 50%, depending on the character code set being used. 3. Running the grep command on a special file produces unpredictable results and is discouraged. Processed August 5, 1991 GREP, EGREP, FGREP(1,C) 4 GREP, EGREP, FGREP(1,C) AIX Commands Reference GREP, EGREP, FGREP(1,C) FLAGS -b Precedes each line by the block number on which it was found. Use this flag to help find disk block numbers by context. -c Displays only a count of matching lines. -e pattern Specifies a pattern. This works the same as a simple pattern but is useful when the pattern begins with a - (minus). -f stringfile Specifies a file that contains patterns (egrep) or strings (the fgrep command). -h When multiple files are being processed, suppresses file names (fgrep and egrep only). -i Makes no distinction between uppercase and lowercase characters when making comparisons (the grep and fgrep commands only). -l Lists just the names of files (once) with matching lines. Each file name is separated by a new-line character. -n Precedes each line with its relative line number in the file. -pparsep Displays the entire paragraph containing matched lines. Paragraphs are delimited by paragraph separators, parsep, which are patterns in the same form as the search pattern. Lines containing the paragraph separators are used only as separators; they are never included in the output. If used, the parsep pattern must follow the -p option without a space. If parsep is not used, the default is a blank line. -q Suppresses error messages about inaccessible files (grep only). -s Silent mode. Nothing is printed except error messages. This is useful for checking status. -v Displays all lines except those that match the specified pattern. Returns exit code if lines were found that did not match the pattern. -w The pattern is searched for as a word (as if surrounded by '\<'pattern'\>'). This flag can be used only with the grep command. Processed August 5, 1991 GREP, EGREP, FGREP(1,C) 5 GREP, EGREP, FGREP(1,C) AIX Commands Reference GREP, EGREP, FGREP(1,C) -x Displays lines that match the pattern exactly with no additional characters (the fgrep command only). EXAMPLES 1. To search several files for a simple string of characters: fgrep "strcpy" *.c This command searches for the string "strcpy" in all files in the current directory with names ending in ".c". 2. To count the number of lines that match a pattern: fgrep -c "{" pgm.c fgrep -c "}" pgm.c This command displays the number of lines in "pgm.c" that contain open and close braces. If you do not put more than one "{" or "}" on a line in your C programs, and if the braces are properly balanced, the two numbers displayed are the same. If the numbers are not the same, you can display the lines that contain braces in the order that they occur in the file with: egrep "{|}" pgm.c 3. To use a pattern that contains some of the pattern-matching characters "*", ^, "?", [, ], \(, \), \"{", and \"}": grep "^[a-zA-Z]" pgm.s This command displays all lines in "pgm.s" that begin with a letter. Because the fgrep command does not interpret pattern-matching characters, the following command makes fgrep search only for the string "^[a-zA-Z]" in "pgm.s". fgrep "^[a-zA-Z]" pgm.s 4. To use an extended pattern that contains some of the pattern-matching characters +, "?", |, (, and ): egrep "\( *([a-zA-Zy*|[0-9]*) *\)" my.txt This command displays lines that contain letters in parentheses or digits in parentheses, but not parenthesized letter-digit combinations. It matches "(y)" and "( 783902)", but not "(alpha19c)". Note: When using the egrep command, \( and \) match parentheses in the text, but ( and ")" are special characters that group parts of the pattern. The reverse is true for grep. Processed August 5, 1991 GREP, EGREP, FGREP(1,C) 6 GREP, EGREP, FGREP(1,C) AIX Commands Reference GREP, EGREP, FGREP(1,C) 5. To display all lines that do not match a pattern: grep -v "^#" pgm.c This command displays all lines in pgm.c that do not begin with a # character. 6. To display the names of files that contain a pattern: fgrep -l "strcpy" *.c This command searches the files in the current directory that end with ".c" and displays the names of those files that contain the string "strcpy". 7. To display all lines containing "$" using grep: grep \\$ file.foo This command demonstrates how to quote a special character to find the literal character, the "$" in this example. RELATED INFORMATION See the following commands: "ed, red," "sed," and "sh, Rsh." See "Introduction to International Character Support" in Managing the AIX Operating System. Processed August 5, 1991 GREP, EGREP, FGREP(1,C) 7