1GREP(1)                     General Commands Manual                    GREP(1)
2
3
4

NAME

6       grep, egrep, fgrep - print lines matching a pattern
7

SYNOPSIS

9       grep [options] PATTERN [FILE...]
10       grep [options] [-e PATTERN | -f FILE] [FILE...]
11

DESCRIPTION

13       Grep  searches the named input FILEs (or standard input if no files are
14       named, or the file name - is given) for lines containing a match to the
15       given PATTERN.  By default, grep prints the matching lines.
16
17       In addition, two variant programs egrep and fgrep are available.  Egrep
18       is the same as grep -E.  Fgrep is the same as grep -F.
19

OPTIONS

21       -A NUM, --after-context=NUM
22              Print NUM  lines  of  trailing  context  after  matching  lines.
23              Places  a  line  containing  --  between  contiguous  groups  of
24              matches.
25
26       -a, --text
27              Process a binary file as if it were text; this is equivalent  to
28              the --binary-files=text option.
29
30       -B NUM, --before-context=NUM
31              Print  NUM  lines  of  leading  context  before  matching lines.
32              Places  a  line  containing  --  between  contiguous  groups  of
33              matches.
34
35       -C NUM, --context=NUM
36              Print  NUM lines of output context.  Places a line containing --
37              between contiguous groups of matches.
38
39       -b, --byte-offset
40              Print the byte offset within the input file before each line  of
41              output.
42
43       --binary-files=TYPE
44              If the first few bytes of a file indicate that the file contains
45              binary data, assume that the file is of type TYPE.  By  default,
46              TYPE is binary, and grep normally outputs either a one-line mes‐
47              sage saying that a binary file matches, or no message  if  there
48              is  no  match.   If  TYPE  is without-match, grep assumes that a
49              binary file does not match; this is equivalent to the -I option.
50              If  TYPE  is  text,  grep  processes a binary file as if it were
51              text; this is  equivalent  to  the  -a  option.   Warning:  grep
52              --binary-files=text  might output binary garbage, which can have
53              nasty side effects if the output is a terminal and if the termi‐
54              nal driver interprets some of it as commands.
55
56       --colour[=WHEN], --color[=WHEN]
57              Surround  the matching string with the marker find in GREP_COLOR
58              environment variable. WHEN may be `never', `always', or `auto'
59
60       -c, --count
61              Suppress normal output; instead print a count of matching  lines
62              for  each  input  file.  With the -v, --invert-match option (see
63              below), count non-matching lines.
64
65       -D ACTION, --devices=ACTION
66              If an input file is a device, FIFO  or  socket,  use  ACTION  to
67              process  it.   By  default,  ACTION  is  read,  which means that
68              devices are read just as if they were ordinary files.  If ACTION
69              is skip, devices are silently skipped.
70
71       -d ACTION, --directories=ACTION
72              If  an  input file is a directory, use ACTION to process it.  By
73              default, ACTION is read, which means that directories  are  read
74              just  as if they were ordinary files.  If ACTION is skip, direc‐
75              tories are silently skipped.  If ACTION is recurse,  grep  reads
76              all  files under each directory, recursively; this is equivalent
77              to the -r option.
78
79       -E, --extended-regexp
80              Interpret PATTERN as an extended regular expression (see below).
81
82       -e PATTERN, --regexp=PATTERN
83              Use PATTERN as the pattern; useful to protect patterns beginning
84              with -.
85
86       -F, --fixed-strings
87              Interpret  PATTERN as a list of fixed strings, separated by new‐
88              lines, any of which is to be matched.
89
90       -P, --perl-regexp
91              Interpret PATTERN as a Perl regular expression.
92
93       -f FILE, --file=FILE
94              Obtain patterns from FILE, one per line.  The  empty  file  con‐
95              tains zero patterns, and therefore matches nothing.
96
97       -G, --basic-regexp
98              Interpret  PATTERN  as  a  basic regular expression (see below).
99              This is the default.
100
101       -H, --with-filename
102              Print the filename for each match.
103
104       -h, --no-filename
105              Suppress the prefixing of  filenames  on  output  when  multiple
106              files are searched.
107
108       --help Output a brief help message.
109
110       -I     Process  a  binary  file as if it did not contain matching data;
111              this is equivalent to the --binary-files=without-match option.
112
113       -i, --ignore-case
114              Ignore case distinctions in  both  the  PATTERN  and  the  input
115              files.
116
117       -L, --files-without-match
118              Suppress  normal  output;  instead  print the name of each input
119              file from which no output would normally have been printed.  The
120              scanning will stop on the first match.
121
122       -l, --files-with-matches
123              Suppress  normal  output;  instead  print the name of each input
124              file from which output would normally have  been  printed.   The
125              scanning will stop on the first match.
126
127       -m NUM, --max-count=NUM
128              Stop  reading  a file after NUM matching lines.  If the input is
129              standard input from a regular file, and NUM matching  lines  are
130              output,  grep  ensures  that the standard input is positioned to
131              just after the last matching line before exiting, regardless  of
132              the  presence of trailing context lines.  This enables a calling
133              process to resume a search.  When grep stops after NUM  matching
134              lines,  it  outputs  any trailing context lines.  When the -c or
135              --count option is also  used,  grep  does  not  output  a  count
136              greater  than NUM.  When the -v or --invert-match option is also
137              used, grep stops after outputting NUM non-matching lines.
138
139       --mmap If possible, use the mmap(2) system call to read input,  instead
140              of  the default read(2) system call.  In some situations, --mmap
141              yields better performance.  However, --mmap can cause  undefined
142              behavior  (including  core dumps) if an input file shrinks while
143              grep is operating, or if an I/O error occurs.
144
145       -n, --line-number
146              Prefix each line of output with the line number within its input
147              file.
148
149       -o, --only-matching
150              Show only the part of a matching line that matches PATTERN.
151
152       --label=LABEL
153              Displays input actually coming from standard input as input com‐
154              ing from file LABEL.  This is especially useful for  tools  like
155              zgrep, e.g.  gzip -cd foo.gz |grep --label=foo something
156
157       --line-buffered
158              Use line buffering, it can be a performance penality.
159
160       -q, --quiet, --silent
161              Quiet;  do  not write anything to standard output.  Exit immedi‐
162              ately with zero status if any match is found, even if  an  error
163              was detected.  Also see the -s or --no-messages option.
164
165       -R, -r, --recursive
166              Read all files under each directory, recursively; this is equiv‐
167              alent to the -d recurse option.
168
169         --include=PATTERN
170              Recurse in directories only searching file matching PATTERN.
171
172         --exclude=PATTERN
173              Recurse in directories skip file matching PATTERN.
174
175       -s, --no-messages
176              Suppress error messages about nonexistent or  unreadable  files.
177              Portability note: unlike GNU grep, traditional grep did not con‐
178              form to POSIX.2, because traditional grep lacked a -q option and
179              its  -s option behaved like GNU grep's -q option.  Shell scripts
180              intended to be portable to traditional grep should avoid both -q
181              and -s and should redirect output to /dev/null instead.
182
183       -U, --binary
184              Treat  the  file(s) as binary.  By default, under MS-DOS and MS-
185              Windows, grep guesses the file type by looking at  the  contents
186              of  the first 32KB read from the file.  If grep decides the file
187              is a text file, it strips the CR characters  from  the  original
188              file  contents  (to  make  regular expressions with ^ and $ work
189              correctly).  Specifying -U overrules this guesswork, causing all
190              files  to be read and passed to the matching mechanism verbatim;
191              if the file is a text file with CR/LF pairs at the end  of  each
192              line,  this  will  cause some regular expressions to fail.  This
193              option has no effect on platforms other than MS-DOS and  MS-Win‐
194              dows.
195
196       -u, --unix-byte-offsets
197              Report  Unix-style  byte  offsets.   This  switch causes grep to
198              report byte offsets as if the file were  Unix-style  text  file,
199              i.e. with CR characters stripped off.  This will produce results
200              identical to running grep on a Unix machine.  This option has no
201              effect  unless -b option is also used; it has no effect on plat‐
202              forms other than MS-DOS and MS-Windows.
203
204       -V, --version
205              Print the version number of grep to standard error.   This  ver‐
206              sion number should be included in all bug reports (see below).
207
208       -v, --invert-match
209              Invert the sense of matching, to select non-matching lines.
210
211       -w, --word-regexp
212              Select  only  those  lines  containing  matches  that form whole
213              words.  The test is that the matching substring must  either  be
214              at  the  beginning  of  the line, or preceded by a non-word con‐
215              stituent character.  Similarly, it must be either at the end  of
216              the line or followed by a non-word constituent character.  Word-
217              constituent characters are letters, digits, and the underscore.
218
219       -x, --line-regexp
220              Select only those matches that exactly match the whole line.
221
222       -y     Obsolete synonym for -i.
223
224       -Z, --null
225              Output a zero byte (the ASCII  NUL  character)  instead  of  the
226              character  that normally follows a file name.  For example, grep
227              -lZ outputs a zero byte after each  file  name  instead  of  the
228              usual  newline.   This option makes the output unambiguous, even
229              in the presence of file names containing unusual characters like
230              newlines.   This  option  can  be  used  with commands like find
231              -print0, perl -0, sort -z, and xargs  -0  to  process  arbitrary
232              file names, even those that contain newline characters.
233

REGULAR EXPRESSIONS

235       A  regular  expression  is  a  pattern that describes a set of strings.
236       Regular expressions are constructed analogously to  arithmetic  expres‐
237       sions, by using various operators to combine smaller expressions.
238
239       Grep  understands  two different versions of regular expression syntax:
240       “basic” and “extended.”  In GNU grep, there is no difference in  avail‐
241       able  functionality  using  either  syntax.   In other implementations,
242       basic regular expressions are less powerful.  The following description
243       applies  to extended regular expressions; differences for basic regular
244       expressions are summarized afterwards.
245
246       The fundamental building blocks are the regular expressions that  match
247       a single character.  Most characters, including all letters and digits,
248       are regular expressions that match themselves.  Any metacharacter  with
249       special meaning may be quoted by preceding it with a backslash.
250
251       A  bracket  expression is a list of characters enclosed by [ and ].  It
252       matches any single character in that list; if the  first  character  of
253       the  list is the caret ^ then it matches any character not in the list.
254       For example, the regular expression  [0123456789]  matches  any  single
255       digit.
256
257       Within a bracket expression, a range expression consists of two charac‐
258       ters separated by a hyphen.  It matches any single character that sorts
259       between  the  two  characters,  inclusive, using the locale's collating
260       sequence and character set.  For example,  in  the  default  C  locale,
261       [a-d] is equivalent to [abcd].  Many locales sort characters in dictio‐
262       nary order, and in these locales [a-d] is typically not  equivalent  to
263       [abcd];  it  might  be equivalent to [aBbCcDd], for example.  To obtain
264       the traditional interpretation of bracket expressions, you can use  the
265       C locale by setting the LC_ALL environment variable to the value C.
266
267       Finally,  certain  named  classes  of  characters are predefined within
268       bracket expressions, as follows.  Their names are self explanatory, and
269       they   are   [:alnum:],  [:alpha:],  [:cntrl:],  [:digit:],  [:graph:],
270       [:lower:], [:print:], [:punct:], [:space:], [:upper:], and  [:xdigit:].
271       For  example,  [[:alnum:]]  means  [0-9A-Za-z],  except the latter form
272       depends upon the C locale and the ASCII character encoding, whereas the
273       former  is  independent  of  locale  and character set.  (Note that the
274       brackets in these class names are part of the symbolic names, and  must
275       be  included  in addition to the brackets delimiting the bracket list.)
276       Most metacharacters  lose  their  special  meaning  inside  lists.   To
277       include  a literal ] place it first in the list.  Similarly, to include
278       a literal ^ place it anywhere but first.  Finally, to include a literal
279       - place it last.
280
281       The period .  matches any single character.  The symbol \w is a synonym
282       for [[:alnum:]] and \W is a synonym for [^[:alnum]].
283
284       The caret ^ and the dollar sign $ are metacharacters that  respectively
285       match the empty string at the beginning and end of a line.  The symbols
286       \< and \> respectively match the empty string at the beginning and  end
287       of  a  word.   The  symbol \b matches the empty string at the edge of a
288       word, and \B matches the empty string provided it's not at the edge  of
289       a word.
290
291       A regular expression may be followed by one of several repetition oper‐
292       ators:
293       ?      The preceding item is optional and matched at most once.
294       *      The preceding item will be matched zero or more times.
295       +      The preceding item will be matched one or more times.
296       {n}    The preceding item is matched exactly n times.
297       {n,}   The preceding item is matched n or more times.
298       {n,m}  The preceding item is matched at least n  times,  but  not  more
299              than m times.
300
301       Two  regular  expressions  may  be  concatenated; the resulting regular
302       expression matches any string formed by  concatenating  two  substrings
303       that respectively match the concatenated subexpressions.
304
305       Two  regular  expressions  may  be  joined by the infix operator |; the
306       resulting regular expression matches any string matching either  subex‐
307       pression.
308
309       Repetition  takes  precedence  over  concatenation, which in turn takes
310       precedence over alternation.  A whole subexpression may be enclosed  in
311       parentheses to override these precedence rules.
312
313       The  backreference \n, where n is a single digit, matches the substring
314       previously matched by the nth parenthesized subexpression of the  regu‐
315       lar expression.
316
317       In  basic  regular  expressions the metacharacters ?, +, {, |, (, and )
318       lose their special meaning; instead use the  backslashed  versions  \?,
319       \+, \{, \|, \(, and \).
320
321       Traditional  egrep  did not support the { metacharacter, and some egrep
322       implementations support \{ instead, so portable scripts should avoid  {
323       in egrep patterns and should use [{] to match a literal {.
324
325       GNU  egrep  attempts to support traditional usage by assuming that { is
326       not special if it would be the start of an invalid interval  specifica‐
327       tion.   For example, the shell command egrep '{1' searches for the two-
328       character string {1 instead of reporting a syntax error in the  regular
329       expression.  POSIX.2 allows this behavior as an extension, but portable
330       scripts should avoid it.
331

ENVIRONMENT VARIABLES

333       Grep's behavior is affected by the following environment variables.
334
335       A locale LC_foo is specified by examining the three  environment  vari‐
336       ables  LC_ALL,  LC_foo,  LANG, in that order.  The first of these vari‐
337       ables that is set specifies the locale.  For example, if LC_ALL is  not
338       set, but LC_MESSAGES is set to pt_BR, then Brazilian Portuguese is used
339       for the LC_MESSAGES locale.  The C locale is  used  if  none  of  these
340       environment  variables  are  set,  or  if  the  locale  catalog  is not
341       installed, or if grep was not compiled with national  language  support
342       (NLS).
343
344       GREP_OPTIONS
345              This variable specifies default options to be placed in front of
346              any  explicit  options.   For  example,   if   GREP_OPTIONS   is
347              '--binary-files=without-match  --directories=skip', grep behaves
348              as if the two options --binary-files=without-match and  --direc‐
349              tories=skip  had  been  specified  before  any explicit options.
350              Option specifications are separated by whitespace.  A  backslash
351              escapes  the  next  character,  so  it can be used to specify an
352              option containing whitespace or a backslash.
353
354       GREP_COLOR
355              Specifies the marker for highlighting.
356
357       LC_ALL, LC_COLLATE, LANG
358              These variables specify the LC_COLLATE locale, which  determines
359              the  collating sequence used to interpret range expressions like
360              [a-z].
361
362       LC_ALL, LC_CTYPE, LANG
363              These variables specify the LC_CTYPE  locale,  which  determines
364              the type of characters, e.g., which characters are whitespace.
365
366       LC_ALL, LC_MESSAGES, LANG
367              These variables specify the LC_MESSAGES locale, which determines
368              the language that grep uses for messages.  The default C  locale
369              uses American English messages.
370
371       POSIXLY_CORRECT
372              If  set,  grep  behaves  as  POSIX.2  requires;  otherwise, grep
373              behaves more like other GNU  programs.   POSIX.2  requires  that
374              options that follow file names must be treated as file names; by
375              default, such options are permuted to the front of  the  operand
376              list  and  are  treated as options.  Also, POSIX.2 requires that
377              unrecognized options be diagnosed as “illegal”, but  since  they
378              are  not  really against the law the default is to diagnose them
379              as  “invalid”.   POSIXLY_CORRECT  also  disables   _N_GNU_nonop‐
380              tion_argv_flags_, described below.
381
382       _N_GNU_nonoption_argv_flags_
383              (Here  N is grep's numeric process ID.)  If the ith character of
384              this environment variable's value is 1, do not consider the  ith
385              operand  of  grep to be an option, even if it appears to be one.
386              A shell can put this variable in the environment for  each  com‐
387              mand  it runs, specifying which operands are the results of file
388              name wildcard expansion and therefore should not be  treated  as
389              options.   This  behavior  is  available  only  with  the  GNU C
390              library, and only when POSIXLY_CORRECT is not set.
391

DIAGNOSTICS

393       Normally, exit status is 0 if selected lines are found and 1 otherwise.
394       But the exit status is 2 if an error occurred, unless the -q or --quiet
395       or --silent option is used and a selected line is found.
396

BUGS

398       Email bug reports to bug-grep@gnu.org.
399
400       Large repetition counts in the {n,m} construct may cause  grep  to  use
401       lots of memory.  In addition, certain other obscure regular expressions
402       require exponential time and space, and may cause grep to  run  out  of
403       memory.
404
405       Backreferences are very slow, and may require exponential time.
406
407
408
409GNU Project                       2002/01/22                           GREP(1)
Impressum