.\" .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for .\" permission to reproduce portions of its copyrighted documentation. .\" Original documentation from The Open Group can be obtained online at .\" http://www.opengroup.org/bookstore/. .\" .\" The Institute of Electrical and Electronics Engineers and The Open .\" Group, have given us permission to reprint portions of their .\" documentation. .\" .\" In the following statement, the phrase ``this text'' refers to portions .\" of the system documentation. .\" .\" Portions of this text are reprinted and reproduced in electronic form .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition, .\" Standard for Information Technology -- Portable Operating System .\" Interface (POSIX), The Open Group Base Specifications Issue 6, .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics .\" Engineers, Inc and The Open Group. In the event of any discrepancy .\" between these versions and the original IEEE and The Open Group .\" Standard, the original IEEE and The Open Group Standard is the referee .\" document. The original Standard can be obtained online at .\" http://www.opengroup.org/unix/online.html. .\" .\" This notice shall appear on any product containing this material. .\" .\" The contents of this file are subject to the terms of the .\" Common Development and Distribution License (the "License"). .\" You may not use this file except in compliance with the License. .\" .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE .\" or http://www.opensolaris.org/os/licensing. .\" See the License for the specific language governing permissions .\" and limitations under the License. .\" .\" When distributing Covered Code, include this CDDL HEADER in each .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE. .\" If applicable, add the following below this CDDL HEADER, with the .\" fields enclosed by brackets "[]" replaced with your own identifying .\" information: Portions Copyright [yyyy] [name of copyright owner] .\" .\" .\" Copyright 1989 AT&T .\" Portions Copyright (c) 1992, X/Open Company Limited All Rights Reserved .\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved .\" Copyright 2018 Nexenta Systems, Inc. .\" Copyright 2020 Oxide Computer Company .\" .Dd August 13, 2020 .Dt GREP 1 .Os .Sh NAME .Nm grep , .Nm egrep , .Nm fgrep .Nd search a file for a pattern .Sh SYNOPSIS .Nm grep .Op Fl E Ns | Ns Fl F .Op Fl bchHilLnorRsqvwx .Op Fl A Ar num .Op Fl B Ar num .Op Fl C Ar num Ns | Ns Fl Ns Ar num .Op Fl -label Ns = Ns Ar name .Oo Fl e Ar pattern_list Oc Ns ... .Oo Fl f Ar pattern_file Oc Ns ... .Op Ar pattern_list .Oo Ar file Oc Ns ... .Sh DESCRIPTION The .Nm utility searches text files for a pattern and prints all lines that contain that pattern. If no files are specified, .Nm assumes standard input. Normally, each line found is copied to standard output. The file name is printed before each line found if there is more than one input file. .Pp .Nm handles patterns as basic regular expressions (BREs); .Nm egrep .Pq same as Nm Fl E handles patterns as extended regular expressions (EREs); .Nm fgrep .Pq same as Nm Fl F handles patterns as fixed strings. .Sh OPTIONS The following options are supported: .Bl -tag -width Ds .It Fl A Ar num Prints .Ar num input lines of context after each matching line. If there are multiple matching lines, their context lines are separated by a .Ql -- delimiter line. .It Fl b Precedes each line by the block number on which it was found. This can be useful in locating block numbers by context (first block is 0). .It Fl B Ar num Prints .Ar num input lines of context before each matching line. If there are multiple matching lines, their context lines are separated by a .Ql -- delimiter line. .It Fl c Prints only a count of the lines that contain the pattern. Overrides .Fl l and .Fl L . .It Fl C Ar num Ns \&, Fl Ns Ar num Prints .Ar num input lines of context before and .Ar number input lines of context after each matching line. If there are multiple matching lines, their context lines are separated by a .Ql -- delimiter line. .It Fl e Ar pattern_list Specifies one or more patterns to be used during the search for input. Patterns in .Ar pattern_list must be separated by a NEWLINE character. A null pattern can be specified by two adjacent newline characters in .Ar pattern_list . Unless the .Fl E or .Fl F option is also specified, each pattern is treated as a BRE, as described in .Xr regex 5 . .It Fl E Matches using extended regular expressions. Treats each pattern specified as an ERE, as described in .Xr regex 5 . If any entire ERE pattern matches an input line, the line is matched. A null ERE matches every line. .It Fl f Ar pattern_file Reads one or more patterns from the file named by the path name .Ar pattern_file . Patterns in .Ar pattern_file are terminated by a NEWLINE character. A null pattern can be specified by an empty line in .Ar pattern_file . Unless the .Fl E or .Fl F option is also specified, each pattern is treated as a BRE, as described in .Xr regex 5 . .It Fl F Matches using fixed strings. Treats each pattern specified as a string instead of a regular expression. If an input line contains any of the patterns as a contiguous sequence of bytes, the line is matched. A null string matches every line. .It Fl h Prevents the name of the file containing the matching line from being prepended to that line. Used when searching multiple files. .It Fl H Precedes each line by the name of the file containing the matching line. .It Fl i Ignores upper/lower case distinction during comparisons. .It Fl -label Ns = Ns Ar name When the name of the matching file is printed .Pq Fl H , instead of printing the string .Ql (standard input) the string .Ar name is printed instead. See .Sx Example 5 . .It Fl l Prints only the names of files with matching lines, separated by NEWLINE characters. Does not repeat the names of files when the pattern is found more than once. If both .Fl l and .Fl L are specified, only the last specified takes effect. Overrides .Fl H . .It Fl L The opposite of the .Fl l flag. Prints only the names of files without matching lines. If both .Fl l and .Fl L are specified, only the last specified takes effect. Overrides .Fl H . .It Fl n Precedes each line by its line number in the file (first line is 1). .It Fl o Prints only the matching part of a line. If a pattern appears more than once in a line, it will be matched and printed multiple times. .Pp The .Fl o option is overridden when any of the .Fl l , .Fl L , or .Fl c options are specified. When the .Fl o option is specified, all context options are ignored. The .Fl o and .Fl v options are not supported together at this time. .It Fl q Quiet. Does not write anything to the standard output, regardless of matching lines. Exits with zero status if an input line is selected. Overrides .Fl c , .Fl l , and .Fl L . .It Fl r Read all files under each directory, recursively. Follow symbolic links on the command line, but skip symlinks that are encountered recursively. If file is a device, FIFO, or socket, skip it. .It Fl R Read all files under each directory, recursively, following all symbolic links. .It Fl s Suppresses error messages about nonexistent or unreadable files. .It Fl v Prints all lines except those that contain the pattern. .It Fl w Searches for the expression as a word as if surrounded by .Ql \e< and .Ql \e> . .It Fl x Considers only input lines that use all characters in the line to match an entire fixed string or regular expression to be matching lines. .El .Sh OPERANDS The following operands are supported: .Bl -tag -width Ds .It Ar file A path name of a file to be searched for the patterns. If no .Ar file operands are specified, the standard input is used. .It Ar pattern_list Specifies one or more patterns to be used during the search for input. This operand is treated as if it were specified as .Fl e Ar pattern_list . Should not be specified if either .Fl e or .Fl f is specified. .El .Sh USAGE Be careful using the characters .Ql $ , .Ql * , .Ql \&[ , .Ql ^ , .Ql | , .Ql \&( , .Ql \&) , and .Ql \e in the .Ar pattern_list because they are also meaningful to the shell. It is safest to enclose the entire .Ar pattern_list in single quotes: .Li '...' . .Pp The .Fl e Ar pattern option has the same effect as the .Ar pattern operand, but is useful when .Ar pattern begins with the hyphen delimiter. It is also useful when it is more convenient to provide multiple patterns as separate arguments. .Pp Multiple .Fl e and .Fl f options are accepted and .Nm uses all of the patterns it is given while matching input text lines. Notice that the order of evaluation is not specified. If an implementation finds a null string as a pattern, it is allowed to use that pattern first, matching every line, and effectively ignore any other patterns. .Pp The .Fl q option provides a means of easily determining whether or not a pattern (or string) exists in a group of files. When searching several files, it provides a performance improvement (because it can quit as soon as it finds the first match) and requires less care by the user in choosing the set of files to supply as arguments (because it exits zero if it finds a match even if .Nm detected an access or read error on earlier file operands). .Ss Large File Behavior See .Xr largefile 5 for the description of the behavior of .Nm when encountering files greater than or equal to 2 Gbyte (2^31 bytes). .Sh EXIT STATUS The following exit values are returned: .Bl -tag -width Ds .It Sy 0 One or more matches were found. .It Sy 1 No matches were found. .It Sy 2 Syntax errors or inaccessible files (even if matches were found). .El .Sh EXAMPLES .Bl -tag -width Ds .It Sy Example 1 No Finding All Uses of a Word To find all uses of the word .Ql Posix (in any case) in the file .Pa text.mm , and write with line numbers: .Bd -literal $ grep -i -n posix text.mm .Ed .It Sy Example 2 No Finding All Empty Lines To find all empty lines in the standard input: .Bd -literal $ grep ^$ .Ed .Pp or .Bd -literal $ grep -v . .Ed .It Sy Example 3 No Finding Lines Containing Strings All of the following commands print all lines containing strings .Ql abc or .Ql def or both: .Bd -literal $ grep 'abc def' $ grep -e 'abc def' $ grep -e 'abc' -e 'def' $ grep -E 'abc|def' $ grep -E -e 'abc|def' $ grep -E -e 'abc' -e 'def' $ grep -E 'abc def' $ grep -E -e 'abc def' $ grep -F -e 'abc' -e 'def' $ grep -F 'abc def' $ grep -F -e 'abc def' .Ed .It Sy Example 4 No Finding Lines with Matching Strings Both of the following commands print all lines matching exactly .Ql abc or .Ql def : .Bd -literal $ grep -E '^abc$ ^def$' $ grep -F -x 'abc def' .Ed .It Sy Example 5 No Using Fl -label When piping standard input into .Nm , as part of a pipeline, occasionally it can be useful override the file name .Ql (standard input) with something from the pipeline. This would output each matching line instead with the name of the input file. .Bd -literal $ for f in *.gz; do > gzcat $f | grep -H --label=$f foo > done .Ed .El .Sh ENVIRONMENT VARIABLES See .Xr environ 5 for descriptions of the following environment variables that affect the execution of .Nm : .Ev LANG , LC_ALL , LC_COLLATE , LC_CTYPE , LC_MESSAGES , and .Ev NLSPATH . .Sh CODE SET INDEPENDENCE .Sy Enabled .Sh INTERFACE STABILITY .Sy Committed .Sh SEE ALSO .Xr sed 1 , .Xr sh 1 , .Xr attributes 5 , .Xr environ 5 , .Xr largefile 5 , .Xr regex 5 , .Xr standards 5 .Sh STANDARDS The .Nm utility is compliant with the .St -p1003.1-2008 specification with the exception of .Fl s option being the same as .Fl q in current implementation for historic reasons. The flags .Op Fl AbBChHrRw are extensions to that specification. .Sh NOTES The results are unspecified if input files contain lines longer than .Dv LINE_MAX bytes or contain binary data. .Dv LINE_MAX is defined in .In limits.h . .Pp Portable applications should use .Nm Fl E and .Nm Fl F instead of .Nm egrep and .Nm fgrep , respectively. .Sh HISTORY The .Nm grep command first appeared in .At v6 . .Pp In the past .Pa /usr/bin/grep , .Pa /usr/bin/egrep , and .Pa /usr/bin/fgrep were separate implementations, and were not standard conforming, with standard conforming ones installed as .Pa /usr/xpg4/bin/grep , .Pa /usr/xpg4/bin/egrep , and .Pa /usr/xpg4/bin/fgrep , respectively. Now all non-conforming implementations are removed, and the ones previously found in .Pa /usr/xpg4/bin are installed in .Pa /usr/bin .