grep [options] pattern [file...]
grep [options] [-e pattern | -f patternFile] [file...]
Searches the input files
(or STDIN if no files are
named, or if file is - )
for lines containing a match to the pattern.
-G --basic-regexp pattern is a Basic Regular Expression (default).
?, +, {, |, ( and ) have no special meaning
-E --extended-regexp pattern is an Extended Regular Expression.
egrep is the same as grep -E.
-F --fixed-strings pattern is a list of fixed strings, separated by newlines, any of which is to be matched.
fgrep is the same as grep -F.
-P --perl-regexp Interpret pattern as a Perl regular expression.
controling matching
| | -v Invert the sense of matching to select non-matching lines. | | -i | |||||||||||||||||||
controlling output
| | -o Show only the part of a line that matches pattern.
| | --color[=when]
Display matches with screen attributes as per the | GREP_COLOR environment variable.Foreground: black 30, green 32, yellow 33, blue 34, purple 35, cyan 36, white 37, red 31 (default) Background: black 40, green 42, yellow 43, blue 44, purple 45, underscore 4, [blink] 5, inverse 7 codes can be combined using ;
as in export GREP_COLOR="33;44"
Codes are not output when inapropriate (like going to a file or pipe) unless
Used with commands | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Restricting Processing
| | -C num limit output to num lines
| | -m num
stop reading a file after num matching lines. | If the input is standard input from a regular file, and num matching lines are output, the standard input is positioned to just after the last matching line. This enables a calling process to resume a search.
When
When used with | ||||||||
Directory processing
| | -R Read all files under each directory, recursively; equivalent to | -d recurse .
| -d action
If an input is a directory, use action to process it. | recurse read all files under each directory, recursively; equivalent to | ||||||||||||
Special handling
| |
If the first few bytes of a file indicate that the file contains
binary data, outputs either a one-line message saying that a binary file matches, | or no message if there is no match.
STTY SANE
| -a Process a binary file as if it were ASCII text | equivalent to ‑‑binary‑files=text.
| -I Ignore a binary file (treat it as if it did not contain matching data ) | equivalent to ‑‑binary‑files=without‑match.
| -U Treat the file(s) as binary. | By default, under MS-DOS and MS-Windows, grep guesses the file type by looking at the contents of the first 32KB read from the file. If grep decides the file is a text file, it strips the CR characters from the original file contents (to make regular expressions with ^ and $ work
correctly). Specifying -U causes all files to be read and passed to the matching mechanism verbatim;
if the file is a text file with CR/LF pairs at the end of each
line, this will cause some regular expressions to fail.
only effective under MS-DOS and MS-Windows unless used with -b .
| ||||||||
--help
| |
-V | Display the version to standard error. |
--mmap | use the mmap system call to read input, instead of read which may yield better performance.
This can cause undefined behavior if an input file shrinks, or if an I/O error occurs.
|
-D action |
If an input file is a device, FIFO or socket, use action to process it.The default action is read, which means that devices are read as ordinary files.If action is skip, devices are silently skipped.
|
--line-buffered | can have a performance penality. |
Basic Regular Expressions:
?, +, {, |, ( and ) have no special meaning* is a meta character.
|
GREP_OPTIONS are options to be placed before any explicit options.GREP_OPTIONS='--color'
GREP_COLOR Specifies the color for highlighting.
A locale LC_xxx is specified by examining:
LC_ALL, LC_xxx, LANG, in order.
The first of these variables that is set specifies the locale.
For example, if LC_ALL is not
set, but LC_MESSAGES is set to pt_BR, then BrazilianPortuguese is used.
The C locale is used if none of these are set, or if the locale catalog is not
installed, or if grep was not compiled with national language support (NLS).
LC_ALL, LC_COLLATE, LANG collating sequence used to interpret range expressions like [a-z].
LC_ALL, LC_CTYPE, LANG type of characters, e.g., which characters are whitespace.
LC_ALL, LC_MESSAGES, LANG language for messages. The default C locale uses American English messages.
POSIXLY_CORRECT
If set, grep behaves as POSIX.2 requires; otherwise, grep
behaves more like other GNU programs.
_N_GNU_nonoption_argv_flags_
(N is grep's numeric process ID.)
If the ith character of this environment variable's value is 1,
do not consider the ith operand to be an option, even if it appears to be one.
A shell can put this variable in the environment for each command it runs,
specifying which operands are the results of file
name wildcard expansion and therefore should not be treated as
options.
Only with the GNU C library, and only when POSIXLY_CORRECT is not set.
Diagnostics
0 if selected lines are found and
1 no lines selected.
2 an error occurred like: no permission or file not found.
Large repetition counts in the {n,m} construct may cause grep to use lots of memory. Certain obscure regular expressions require exponential time and space.
Backreferences are very slow, and require exponential time.
Current "official" GNU Doc
See also egrep, fgrep, sed, sh, attributes, environ, largefile, regex, regexp, XPG4
GNU Project 2002/01/22 GREP(1)
|
From SUN
/usr/bin/grep [ -bchilnsvw ] limited-regular-expression [ filename ... ]
/usr/xpg4/bin/grep [ -E | -F ] [ -c | -l | -q ] [ -bhinsvwx* ] ... pattern [ file ... ]
... -e pattern_list ... [ -f pattern_file ] ... searches the input for a pattern and may display lines that contain that pattern.
Normally, each line found is copied to standard output. grep uses limited regular expressions like those described on the regexp(5) manual page to match the patterns.
/usr/xpg4/bin/grep
-E pattern_list as a full regular expression -F pattern_list as a fixed string.
Since characters
OPTIONS-v reVerse the sense of the operation. i.e. display all lines not containing the pattern.-i ignore case -n Precede each line by its line number in the file
(first line is 1).
-c only display a count of the lines that contain the pattern or-cv count of lines not matching.
-h hide the name of the file containing the matchingline normally appended to that line when searching multiple files. -l display unique list of the names of files with matching lines, separated by \n characters. -s Suppress error messages about nonexistent or unreadable files.-w search for the expression as a word as if surrounded by \< and \>-b Precede each line by the block number on which it was
found, useful in locating block numbers by context (first block is 0).
for /usr/xpg4/bin/grep only aka egrep : -e pattern_listSpecify one or more patterns to be used during the search for input. patterns in pattern_list are separated by a NEWLINE character. A null pattern is specified by two adjacent newline characters in pattern_list. Unless the -E or -F is also specified, each pattern will be
treated as a basic regular expression. Multiple -e and -f options are accepted .
All of the specified patterns are used when matching lines,
but the order of evaluation is unspecified.
followed byThe order of precedence of operators is [ ], then * ? +,
then concatenation, then | and new-line.
Operandsfile If no file operands are specified, the standard input will be used.
USAGEThe-epattern_list is useful when pattern_list begins
with the hyphen and has the same effect as the
pattern_list operand. Useful when it is more
convenient to provide multiple patterns as separate arguments.
Multiple
The ExamplesFind all uses of the "Posix" (-i ignoring case) in the
file text.mm, and write lines with line numbers( -n ):grep -i -n posix text.mm
Display line numbers( -n ) containg empty lines ( i.e where beginning is immediately followed by end of line)
Display all lines containing strings abc or def or both :
Both of the following commands display all lines matching exactly
Exit status0 sucessfully found one or more matches .1 failed to find any matches. 2 Syntax errors or inaccessible files (even if some matches were found). Environment VariablesSee environ for the following environment variables : LC_COLLATE, LC_CTYPE, LC_MESSAGES, and NLSPATH.NotesLarge File Behavior.See largefile(5) for the description of the behavior of grep
when encountering files greater than or equal to 2 Gbyte ( 2**31 bytes).
Lines are limited only by the size of the available virtual
memory. If there is a line with embedded nulls, grep will
only match up to the first null; if it matches, it will display the entire line.
The results are unspecified if input files contain lines
longer than LINE_MAX bytes or contain binary data. |