grep [options] pattern [file...]
grep [options] [-e pattern | -f patternFile] [file …]
bzgrep, zgrep
handle compressed files.
file
s
(or STDIN if no files are
named, or if file
is - )
for lines containing a match to the pattern
.
-G
is a Basic Regular Expression (default).
--basic-regexp pattern
?
, +
, {
, |
, (
and )
must be preceeded by \
to ENABLE special meaning
-E
is an Extended Regular Expression.
--extended-regexp pattern
This adds ?
, +
, and |
, and it removes the need to escape ( ) and { }
.
Which permits, for example, matching EITHER patternA | patternB
.
egrep
is the same as grep -E
.
-F
is a list of fixed strings, separated by newlines, any of which is to be matched.
--fixed-strings pattern
fgrep
is the same as grep -F
.
-P
Interpret
--perl-regexppattern
as a
Perl regular expression.
controling matching
| -v Invert the sense of matching to | select non-matching lines. | -i |
controlling output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-q | Do not write to STDOUT .Errors like " Permission denied " are written to STDERR and return code is 2.Exit with zero status as soon as a match is found. If no match return code is 1 (Useful in conditional statments.) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-s | Suppress error
messages about nonexistent, unaccessable files or a directory normally sent to STDERR .
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-o | only the matching part of the line is output. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
--color[=when]
| when may be never , always , or auto
Matched sections are displayed using
Examples:
Codes are not output when inappropriate (For example: to a file or pipe) unless
Example:Show 2 lines before
With Prefix each line with the line number
Used with commands |
Directory processing | ||||||||
-d action
| If an input is a directory:action : read : as ordinary files, default. skip . recurse : reads all files under each directory, recursively.
| |||||||
-R | equivalent to -d recurse .
| |||||||
--include=pattern | Recurse only searching file matching pattern.
|
binary file handling | ||||||||
|
If the first few bytes of a file indicate it contains binary data (non-ASCII text†), On a match, outputs Binary file name matches .
Control characters sent to the terminal can set attributes making it unreadable. If this happens try | |||||||
-a | Process all file as text ‑‑binary‑files=text equivalent.
| |||||||
-I | binary files do not match.‑‑binary‑files=without‑match equivalent.
| |||||||
-U | Process file (s) as binary.
If the file appears to be a text file, By default, under MS-Windows, uses the first 32KB of the file to determine if a file is text. |
--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. May 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.read , which means that devices are read as ordinary files (default).skip , devices are silently skipped.
|
| flush after each match. This permits piping to process each match as it occurs rather than waiting for full buffer.
has a performance penality. |
Basic Regular Expressions:
? , + , { , | , ( and ) alone have no special meaningUse backslash to ENABLE special meaning (ex: \? )* is a meta character.
|
Regular ExpressionsApattern describes a set of strings, using operators to combine smaller expressions.
The simplest regular expression matchs a single character. For example:
Regular expressions joined by
Metacharacters, can be treated as normal
character by preceding it with a
repetition operators may follow a regular expression.
\> matches the empty string at the beginning of a word, \< … at the end of a word. \b matches the empty string at the edge of a word, \B matches the empty string provided it's not at the edge of a word.
subexpression is defined using
backreference
Precedence: Repetition, concatenation, alternation. |
$GREP_OPTIONS
are placed before explicit options specified on the command.$GREP_OPTIONS='--color'
$GREP_COLORS
Specifies the color for highlighting. GREP_COLOR
†
Colors and attributes used for highlighting.
fb
ForegroundColor BackgroundColor
cx=fb |
fb
: Select Graphic Rendition (SGR) forgroundBackground
integers concatenated with semicolons. ANSI).
Common values include 1 for bold, 4 for underline, 5 for blink, 7 for inverse, 39 for default foreground color,
30 to 37 for foreground colors,
90 to 97 for foreground colors,
38;5;0 to 38;5;255 for 88-color and 256-color modes
foreground colors, 49 for default background color, 40 to 47 for background colors, 100 to 107 for 16-color mode background
colors, and 48;5;0 to 48;5;255 for 88-color and 256-color modes background colors.
The 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 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.
if [ $? != 1 ];then …
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 grep
See also egrep, fgrep, sed, sh, attributes, environ, largefile, regex, regexp, XPG4
-i
ignoring case) in the
file text.mm
, and write lines with line numbers( -n ):
Display line numbers( -n ) containg empty lines ( i.e where beginning is immediately followed by end of line)
grep -n ^$ or grep -n -v .
Display all lines containing strings abc or def or both :
grep -E 'abc def' -or- grep -F 'abc def'
Both of the following commands display all lines matching exactly abc
or def
:
grep -E '^abc$ ^def$' -or- grep -F -x 'abc def'
To find an A
surrounded by tabs, using ANSI-C
quoting for bash
use:
grep $'\tA\t'
Change a line beginning with hours:minutes
(where hours
may be one or two digits) to be zero filled to two digits if it was only one digit:
Green defines a subexpression, brown uses the subexpression
> echo "8:27 stuff stuff" |sed "s/^\(.\):/0\1:/" #
Take first any character, folllowed by a colon, substitute a 0 before it and a colon
08:27 stuff stuff
> echo "19:42 stuff stuff" |sed "s/^\(.\):/0\1:/"
# no change as there are 2 characters between the start of the line and the colon
19:42 stuff stuff
$LC_COLLATE, $LC_CTYPE, $LC_MESSAGES, and $NLSPATH
.
The results are unspecified if input files contain binary data or
lines longer than LINE_MAX
(2048) bytes (defined in /usr/include/sys/syslimits.h or /usr/include/limits.h
Large File Behavior.
International Components for Unicode
See
egrep, fgrep, sed,
sh, attributes(5),
environ(5), largefile(5),
regex(5), regexp(5),
XPG4(5)
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.