GREP(1P) | POSIX Programmer's Manual | GREP(1P) |
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
grep — search a file for a pattern
grep [-E|-F] [-c|-l|-q] [-insvx] -e pattern_list
[-e pattern_list]... [-f pattern_file]... [file...]
grep [-E|-F] [-c|-l|-q] [-insvx] [-e pattern_list]...
-f pattern_file [-f pattern_file]... [file...]
grep [-E|-F] [-c|-l|-q] [-insvx] pattern_list [file...]
The grep utility shall search the input files, selecting lines matching one or more patterns; the types of patterns are controlled by the options specified. The patterns are specified by the -e option, -f option, or the pattern_list operand. The pattern_list's value shall consist of one or more patterns separated by <newline> characters; the pattern_file's contents shall consist of one or more patterns terminated by a <newline> character. By default, an input line shall be selected if any pattern, treated as an entire basic regular expression (BRE) as described in the Base Definitions volume of POSIX.1‐2017, Section 9.3, Basic Regular Expressions, matches any part of the line excluding the terminating <newline>; a null BRE shall match every line. By default, each selected input line shall be written to the standard output.
Regular expression matching shall be based on text lines. Since a <newline> separates or terminates patterns (see the -e and -f options below), regular expressions cannot contain a <newline>. Similarly, since patterns are matched against individual lines (excluding the terminating <newline> characters) of the input, there is no way for a pattern to match a <newline> found in the input.
The grep utility shall conform to the Base Definitions volume of POSIX.1‐2017, Section 12.2, Utility Syntax Guidelines.
The following options shall be supported:
The following operands shall be supported:
The standard input shall be used if no file operands are specified, and shall be used if a file operand is '-' and the implementation treats the '-' as meaning standard input. Otherwise, the standard input shall not be used. See the INPUT FILES section.
The input files shall be text files.
The following environment variables shall affect the execution of grep:
Default.
If the -l option is in effect, the following shall be written for each file containing at least one selected input line:
"%s\n", <file>
Otherwise, if more than one file argument appears, and -q is not specified, the grep utility shall prefix each output line by:
"%s:", <file>
The remainder of each output line shall depend on the other options specified:
"%d\n", <count>
"%d:", <line number>
"%s", <selected-line contents>
The standard error shall be used only for diagnostic messages.
None.
None.
The following exit values shall be returned:
If the -q option is specified, the exit status shall be zero if an input line is selected, even if an error was detected. Otherwise, default actions shall be performed.
The following sections are informative.
Care should be taken when using characters in pattern_list that may also be meaningful to the command interpreter. It is safest to enclose the entire pattern_list argument in single-quotes:
'...'
The -e pattern_list option has the same effect as the pattern_list operand, but is useful when pattern_list begins with the <hyphen-minus> delimiter. It is also useful when it is more convenient to provide multiple patterns as separate arguments.
Multiple -e and -f options are accepted and grep uses all of the patterns it is given while matching input text lines. (Note 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.)
The -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 grep detected an access or read error on earlier file operands).
When using grep to process pathnames, it is recommended that LC_ALL, or at least LC_CTYPE and LC_COLLATE, are set to POSIX or C in the environment, since pathnames can contain byte sequences that do not form valid characters in some locales, in which case the utility's behavior would be undefined. In the POSIX locale each byte is a valid single-byte character, and therefore this problem is avoided.
grep -i -n posix text.mm
grep ^$
or:
grep -v .
grep -E 'abc|def'
grep -F 'abc def'
grep -E '^abc$|^def$'
grep -F -x 'abc def'
This grep has been enhanced in an upwards-compatible way to provide the exact functionality of the historical egrep and fgrep commands as well. It was the clear intention of the standard developers to consolidate the three greps into a single command.
The old egrep and fgrep commands are likely to be supported for many years to come as implementation extensions, allowing historical applications to operate unmodified.
Historical implementations usually silently ignored all but one of multiply-specified -e and -f options, but were not consistent as to which specification was actually used.
The -b option was omitted from the OPTIONS section because block numbers are implementation-defined.
The System V restriction on using - to mean standard input was omitted.
A definition of action taken when given a null BRE or ERE is specified. This is an error condition in some historical implementations.
The -l option previously indicated that its use was undefined when no files were explicitly named. This behavior was historical and placed an unnecessary restriction on future implementations. It has been removed.
The historical BSD grep -s option practice is easily duplicated by redirecting standard output to /dev/null. The -s option required here is from System V.
The -x option, historically available only with fgrep, is available here for all of the non-obsolescent versions.
None.
sed
The Base Definitions volume of POSIX.1‐2017, Chapter 8, Environment Variables, Chapter 9, Regular Expressions, Section 12.2, Utility Syntax Guidelines
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version 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 .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
2017 | IEEE/The Open Group |