grep(C) 19 June 1992 grep(C) Name grep, egrep, fgrep - search files for a pattern Syntax grep [ -bchilnsvy ] [ -f expfile] [ [-e] expression ] [ files ] egrep [ -bchilnv ] [ -f expfile ] [ [-e] expression ] [ files ] fgrep [ -bclnvxy ] [ -f expfile ] [ [-e] expression ] [ files ] Description Commands of the grep family search the input files (or standard input if no files are specified) for lines matching a pattern. Normally, each matching line is copied to the standard output. If more than one file is being searched, the name of the file in which each match occurs is also written to the standard output along with the matching line (unless the -h option is used, see below). grep patterns are limited regular expressions in the style of ed(C). grep uses a compact nondeterministic algorithm. egrep patterns are full regular expressions; it uses a fast deterministic algorithm that sometimes needs exponential space. fgrep patterns are fixed strings. fgrep is fast and compact. The following options are recognized: -v All lines but those matching are displayed. -x Displays only exact matches of an entire line. (fgrep only.) -c Only a count of matching lines is displayed. -l Only the names of files with matching lines are displayed, separated by newlines. -h Prevents the name of the file containing the matching line from being prepended to that line. Used when searching multiple files. (This option works with grep and egrep only.) -n Each line is preceded by its relative line number in the file. -b Each line is preceded by the block number on which it was found. This is sometimes useful in locating disk block numbers by context. -s Suppresses error messages produced for nonexistent or unreadable files. (grep only.) Note that the -s option will not suppress error messages generated by the -f option. -i Turns on matching of letters of either case in the input so that case is insignificant. Conversion between uppercase and lowercase letters is dependent on the locale setting. -y Turns on matching of letters of either case in the input so that case is insignificant. Conversion between uppercase and lowercase letters is dependent on the locale setting. -y does not work with egrep. Note: -y is not a standard UNIX system option. It is maintained for backwards compatibility with XENIX. -e expression or strings Same as a simple expression argument, but useful when the expres- sion begins with a dash (-). -f expfile The regular expression for grep or egrep, or strings list for fgrep is taken from the expfile. In all cases (except with -h) the filename is output if there is more than one input file. Care should be taken when using the characters $ * [ ^ | ( ) and \ in expression, because they are also meaningful to the shell. It is safest to enclose the entire expression or strings argument in single quotation marks. For example: grep '[Ss]omeone' text.file This command would find all lines containing the word ``someone'' in the file text.file, whether the initial ``s'' is uppercase or lowercase. Multiple strings can be specified in fgrep without using a separate strings file by using the quoting conventions of the shell to imbed new- lines in the string argument. For example, if you were using the Bourne shell (sh(C)) you might enter the following on the command line: fgrep 'Someone someone' text.file This would have the same effect as the grep example above. See the csh(C) manual page for ways to imbed newlines in a string when using csh(C). egrep accepts regular expressions as in ed(C), with the addition of the following: + A regular expression followed by a plus sign (+) matches one or more occurrences of the regular expression. + A regular expression followed by a question mark (?) matches 0 or 1 occurrences of the regular expression. + Two regular expressions separated by a vertical bar (|) or by a new- line match strings that are matched by either regular expression. + A regular expression may be enclosed in parentheses ``( )'' for group- ing. For example: egrep '([Ss]ome|[Aa]ny)one' text.file This example displays all lines in text.file containing the words ``someone'' or ``anyone'', whether or not they are spelled with ini- tial capital letters. Without the parentheses, this example would display all lines containing the words ``some'' or ``anyone'' (because the vertical bar (|) operator is of lower precedence than concatena- tion, see below). Because of the algorithm used, egrep does not support extended ranges as in ed(C): Ranges like [a-z] are interpreted on the basis of the ma- chine's collating sequence, not the collating sequence defined by the locale. grep supports col(C) extended ranges. The \( and \) operators, supported by ed(C), are not supported by egrep. The order of precedence of operators is [ ], then * ? +, then concatena- tion, then backslash (\) with newline or vertical bar (|). See also col(C), coltbl(M), ed(C), locale(M), sed(C), sh(C) Diagnostics Exit status is 0 if any matches are found, 1 if no matches are found, and 2 for syntax errors or inaccessible files. Notes Ideally there should be only one grep, but there isn't a single algorithm that spans a wide enough range of space-time tradeoffs. Lines are limited to 256 characters. Longer lines are truncated. When using grep with the -y option, the search is not made totally case insensitive in character ranges specified within brackets. Standards conformance egrep, fgrep and grep are conformant with: AT&T SVID Issue 2; and X/Open Portability Guide, Issue 3, 1989.