grep - search a file for a pattern
grep [-E| -F] [-c| -l| -q] [-bhinsvxyC] -e pattern_list [-f
pattern_file]. . .[file . . .]
grep [-E| -F] [-c| -l| -q] [-bhinsvxyC][-e pattern_list] -f
pattern_file]. . .[file . . .]
grep [-E| -F] [-c| -l| -q] [-bhinsvxyC] pattern_list[file . . .]
The grep utility searches 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 consists of one or
more patterns separated by newline characters; the pattern_file's
contents consist of one or more patterns terminated by newline
characters. By default, an input line will be selected if any pattern,
treated as an entire basic regular expression (BRE) as described in the
regcomp(5) man page under the section titled: Basic Regular Expressions,
matches any part of the line; a null BRE will match every line. By
default, each selected input line will be written to the standard output.
Regular expression matching will be based on text lines. Since a newline
character separates or terminates patterns (see the -e and -f options
below), regular expressions cannot contain a newline character.
Similarly, since patterns are matched against individual lines of the
input, there is no way for a pattern to match a newline character found
in the input.
Command line options are:
-E Match using extended regular expressions. Treat each pattern
specified as an ERE, as described in the regcomp(5) man page
under the section titled: Extended Regular Expressions. If any
entire ERE pattern matches an input line, the line will be
matched. A null ERE matches 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
will be matched. A null string matches every line.
-b Precede each output line by the block number within the input
file where it was found. The first (512 byte) block is number 0.
-c Write only a count of selected lines to standard output.
Specify one or more patterns to be used during the search for
input. Patterns in pattern_list must be separated by a newline
character. 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 will be treated as a BRE, as
described in the regcomp(5) man page under the section titled:
Basic Regular Expressions. Multiple -e and -f options are
accepted by the grep utility. All of the specified patterns are
used when matching lines, but the order of evaluation is
Read one or more patterns from the file named by the pathname
pattern_file. Patterns in pattern_file are terminated by a
newline character. A null pattern can be specified by an empty
line in pattern_file. Unless the -E or -F option is also
specified, each pattern will be treated as a BRE, as described in
the regcomp(5) man page under the section titled: Basic Regular
-h Suppress the name of the file containing the matching line from
being printed on output when multiple files are searched.
-i Perform pattern matching in searches without regard to case. See
the regcomp(5) man page under the section titled: Regular
Expression General Requirements.
-l Write only the names of files containing selected lines to
standard output. Pathnames are written once per file searched.
If the standard input is searched, a pathname of "(standard
input)" will be written, in the POSIX locale. In other locales,
standard input may be replaced by something more appropriate in
-n Precede each output line by its relative line number in the file,
each file starting at line 1. The line number counter will be
reset for each file processed.
-q Quiet. Do not write anything to the standard output, regardless
of matching lines. Exit with zero status if an input line is
-s Suppress the error messages ordinarily written for non-existent
or unreadable files. Other error messages will not be suppressed.
-v Select lines not matching any of the specified patterns. If the
-v option is not specified, selected lines will be those that
match any of the specified patterns.
-x Consider only input lines that use all characters in the line to
match an entire fixed string or regular expression to be matching
-y Obsolescent option. Same as -i.
-C Obsolete option. Unused.
The following operands are supported:
Specify one or more patterns to be used during the search for input.
This operand is 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 will be used.
The standard input will be used only if no file operands are specified.
See INPUT FILES.
The input files must be text files.
If the -l option is in effect, and the -q option is not, the following
will be written for each file containing at least one selected input
Otherwise, if more than one file argument appears, and the -q and -h
options are not set, the grep utility will prefix each output line by:
The remainder of each output line depends on the other options specified:
o If the -c option is in effect, the remainder of each output line will
o If -c is not in effect and the -n option is in effect, the grep
utility will prefix each output line by:
"%d:", <line number>
o If -c is not in effect and the -b option is in effect, the grep
utility will prefix each output line by:
"%d:", <block number>
o Finally, the following will be written to standard output:
"%s", <selected-line contents>
Used only for diagnostic messages.
ed(1), egrep(1), fgrep(1), regcomp(5), sed(1), sh(1)
Exit status is 0 if any matches are found, 1 if none, >1 for syntax
errors or inaccessible files (even if matches were found).
CONSEQUENCES OF ERRORS
If the -q option is specified, the exit status will be zero if an input
line is selected, even if an error was detected.
If the requested action cannot be performed on a file, grep will issue a
diagnostic message to standard error and continue processing the next
operand in sequence, but the final exit status is returned as non-zero.
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
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 will use 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 will exit
zero if it finds a match even if grep detected an access or read error on
earlier file operands).
1. To find all uses of the word Posix in a bunch of files where some of
the files cannot be opened due to permission restrictions:
grep -q -n Posix /etc/*
Note the above use of the -q command line option to grep. If the -q
is not used in the above command line, then when the first file is
encountered that is not readable grep will error terminate even if
there were some matches found.
2. To find all uses of the word Posix (in any case) in file text.mm and
write with line numbers:
grep -i -n posix text.mm
3. To find all empty lines in the standard input:
grep -v .
4. Both of the following commands print all lines containing strings
abc or def or both:
grep -E 'abc
grep -F 'abc
5. Both of the following commands print all lines matching exactly abc
grep -E 'abc$
grep -F -x 'abc
If there is a line with embedded nulls, grep will only match up to the
first null; if it matches, it will print the entire line.
PPPPaaaaggggeeee 5555 [ Back ]