GREP(1P)

๐Ÿ’– GREP(1P) โ€” POSIX Programmer's Manual ๐Ÿ’–

okay bestie, this is the POSIX page for grep, and the manual name is right there: GREP(1P). It's giving "search a file for a pattern" energy, and we're about to get into it. โœจ

โœจ PROLOG โœจ

So this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ โ€” no cap, consult the corresponding Linux manual page for details of Linux behavior โ€” or the interface may not be implemented on Linux at all. ๐Ÿ’€

๐ŸŒธ NAME ๐ŸŒธ

grep โ€” search a file for a pattern. Literally the icon.

๐ŸŽ€ SYNOPSIS ๐ŸŽ€

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...]

๐Ÿ’… DESCRIPTION ๐Ÿ’…

The grep utility shall search the input files, selecting lines matching one or more patterns. The types of patterns are controlled by the options you specify. The patterns come from the -e option, the -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. Periodt.

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. That's just how it is, bestie.

โœจ OPTIONS โœจ

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:

  • -E โ€” Match using extended regular expressions. Treat each pattern specified as an ERE, as described in the Base Definitions volume of POSIX.1โ€2017, Section 9.4, Extended Regular Expressions. If any entire ERE pattern matches some part of an input line excluding the terminating <newline>, the line shall be matched. A null ERE shall match every line.
  • -F โ€” Match using fixed strings. Treat 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 shall be matched. A null string shall match every line.
  • -c โ€” Write only a count of selected lines to standard output.
  • -e pattern_list โ€” Specify one or more patterns to be used during the search for input. The application shall ensure that patterns in pattern_list are separated by a <newline>. A null pattern can be specified by two adjacent <newline> characters in pattern_list. Unless the -E or -F option is also specified, each pattern shall be treated as a BRE, as described in the Base Definitions volume of POSIX.1โ€2017, Section 9.3, Basic Regular Expressions. Multiple -e and -f options shall be accepted by the grep utility. All of the specified patterns shall be used when matching lines, but the order of evaluation is unspecified.
  • -f pattern_file โ€” Read one or more patterns from the file named by the pathname pattern_file. Patterns in pattern_file shall be terminated by a <newline>. A null pattern can be specified by an empty line in pattern_file. Unless the -E or -F option is also specified, each pattern shall be treated as a BRE, as described in the Base Definitions volume of POSIX.1โ€2017, Section 9.3, Basic Regular Expressions.
  • -i โ€” Perform pattern matching in searches without regard to case; see the Base Definitions volume of POSIX.1โ€2017, Section 9.2, Regular Expression General Requirements.
  • -l โ€” (The letter ell.) Write only the names of files containing selected lines to standard output. Pathnames shall be written once per file searched. If the standard input is searched, a pathname of "(standardinput)" shall be written, in the POSIX locale. In other locales, "standardinput" may be replaced by something more appropriate in those locales.
  • -n โ€” Precede each output line by its relative line number in the file, each file starting at line 1. The line number counter shall be reset for each file processed.
  • -q โ€” Quiet. Nothing shall be written to the standard output, regardless of matching lines. Exit with zero status if an input line is selected.
  • -s โ€” Suppress the error messages ordinarily written for nonexistent or unreadable files. Other error messages shall not be suppressed.
  • -v โ€” Select lines not matching any of the specified patterns. If the -v option is not specified, selected lines shall be those that match any of the specified patterns.
  • -x โ€” Consider only input lines that use all characters in the line excluding the terminating <newline> to match an entire fixed string or regular expression to be matching lines.

๐Ÿฆ‹ OPERANDS ๐Ÿฆ‹

The following operands shall be supported:

  • pattern_list โ€” Specify one or more patterns to be used during the search for input. This operand shall be treated as if it were specified as -e pattern_list.
  • file โ€” A pathname of a file to be searched for the patterns. If no file operands are specified, the standard input shall be used.

๐ŸŒˆ STDIN ๐ŸŒˆ

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.

โญ INPUT FILES โญ

The input files shall be text files. Simple as that.

๐Ÿ’œ ENVIRONMENT VARIABLES ๐Ÿ’œ

The following environment variables shall affect the execution of grep:

  • LANG โ€” Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1โ€2017, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)
  • LC_ALL โ€” If set to a non-empty string value, override the values of all the other internationalization variables.
  • LC_COLLATE โ€” Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regular expressions.
  • LC_CTYPE โ€” Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments and input files) and the behavior of character classes within regular expressions.
  • LC_MESSAGES โ€” Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
  • NLSPATH โ€” Determine the location of message catalogs for the processing of LC_MESSAGES.

๐Ÿงš ASYNCHRONOUS EVENTS ๐Ÿงš

Default. That's it. No drama.

๐Ÿ”ฅ STDOUT ๐Ÿ”ฅ

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:

  • If the -c option is in effect, the remainder of each output line shall contain:
"%d\n", <count>
  • Otherwise, if -c is not in effect and the -n option is in effect, the following shall be written to standard output:
"%d:", <line number>
  • Finally, the following shall be written to standard output:
"%s", <selected-line contents>

๐Ÿ’€ STDERR ๐Ÿ’€

The standard error shall be used only for diagnostic messages. That's its whole job.

๐Ÿ“ OUTPUT FILES ๐Ÿ“

None. Nada. Zero. ๐Ÿ’…

๐Ÿ‘‘ EXTENDED DESCRIPTION ๐Ÿ‘‘

None. Nothing extra, bestie.

๐ŸŒŸ EXIT STATUS ๐ŸŒŸ

The following exit values shall be returned:

  • 0 โ€” One or more lines were selected.
  • 1 โ€” No lines were selected.
  • >1 โ€” An error occurred.

๐Ÿ’– CONSEQUENCES OF ERRORS ๐Ÿ’–

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.

๐ŸŽ€ APPLICATION USAGE ๐ŸŽ€

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.


โœจ EXAMPLES โœจ

Okay bestie, this is the part where it all clicks. Real commands, real vibes. ๐Ÿซถ

  1. Wanna find every single use of the word "Posix" (in any case, we're not being picky) inside the file text.mm, and have it spit out line numbers too? Say less:
grep -i -n posix text.mm
  1. Trying to catch all the empty lines chilling in standard input? Two ways, both iconic:
grep ^$

or:

grep -v .
  1. Both of these commands print out all the lines containing the strings "abc" or "def" or both. Two totally different routes, same destination, main character energy all around:
grep -E 'abc|def'
grep -F 'abc
def'
  1. And both of these print all lines matching exactly "abc" or "def" โ€” no extra characters sneaking in, we're being strict:
grep -E '^abc$|^def$'
grep -F -x 'abc
def'

๐Ÿ’ญ RATIONALE ๐Ÿ’ญ

So why does this manpage look the way it does? Grab a snack, I got you. ๐Ÿ“

This grep has been enhanced in an upwards-compatible way so it provides the exact functionality of the historical egrep and fgrep commands too. And no, that wasn't an accident โ€” it was the clear intention of the standard developers to consolidate the three greps into a single command. One grep to rule them all, periodt. ๐Ÿ‘‘

The old egrep and fgrep commands are likely to keep being supported for many years to come as implementation extensions, which lets historical applications keep operating unmodified. So your ancient scripts can keep vibing, no rewrites needed. ๐Ÿ’…

Historical implementations usually silently ignored all but one of multiply-specified -e and -f options โ€” but they were not consistent about which specification was actually used. Messy behaviour, honestly. ๐Ÿ™ˆ

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 the action taken when given a null BRE or ERE is specified. Heads up though: 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. That behavior was historical and placed an unnecessary restriction on future implementations. It has been removed. Bye bestie. ๐Ÿ‘‹

The historical BSD grep -s option practice is easily duplicated by redirecting standard output to /dev/null. But plot twist โ€” the -s option required here is from System V, not BSD.

And the -x option, historically available only with fgrep, is available here for all of the non-obsolescent versions. Glow up fr. โœจ


๐Ÿ”ฎ FUTURE DIRECTIONS ๐Ÿ”ฎ

None.

That's it. That's the whole section. Nothing coming down the pipeline, no crumbs, no hints. She said what she said. ๐Ÿ’€


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

  • 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 .


And that's a wrap on GREP(1P)! ๐ŸŽ€ This edition is brought to you by IEEE/The Open Group, dated 2017 โ€” the source and version behind everything you just read. Go forth and grep responsibly, bestie. You ate. ๐Ÿ’–

๐Ÿ“ฆ where this page comes from