Home
Main website
Display Sidebar
Hide Ads
Recent Changes
View Source:
grep(1)
Edit
PageHistory
Diff
Info
LikePages
GREP !!!GREP NAME SYNOPSIS DESCRIPTION OPTIONS REGULAR EXPRESSIONS ENVIRONMENT VARIABLES DIAGNOSTICS BUGS ---- !!NAME grep, egrep, fgrep - print lines matching a pattern !!SYNOPSIS __grep__ [[''options''] ''PATTERN'' [[''FILE''...]__ grep__ [[''options''] [[__-e__ ''PATTERN'' | __-f__ ''FILE''] [[''FILE''...] !!DESCRIPTION __Grep__ searches the named input ''FILE''s (or standard input if no files are named, or the file name __-__ is given) for lines containing a match to the given ''PATTERN''. By default, __grep__ prints the matching lines. In addition, two variant programs __egrep__ and __fgrep__ are available. __Egrep__ is the same as __grep -E__. __Fgrep__ is the same as __grep -F__. !!OPTIONS __-A__ ''NUM'', __--after-context=__''NUM'' Print ''NUM'' lines of trailing context after matching lines. __-a__, __--text__ Process a binary file as if it were text; this is equivalent to the __--binary-files=text__ option. __-B__ ''NUM'', __--before-context=__''NUM'' Print ''NUM'' lines of leading context before matching lines. __-C__ [[''NUM''], __-__''NUM'', __--context__[[__=__''NUM''] Print ''NUM'' lines (default 2) of output context. __-b__, __--byte-offset__ Print the byte offset within the input file before each line of output. __--binary-files=__''TYPE'' If the first few bytes of a file indicate that the file contains binary data, assume that the file is of type ''TYPE''. By default, ''TYPE'' is __binary__, and __grep__ normally outputs either a one-line message saying that a binary file matches, or no message if there is no match. If ''TYPE'' is __without-match__, __grep__ assumes that a binary file does not match; this is equivalent to the __-I__ option. If ''TYPE'' is __text__, __grep__ processes a binary file as if it were text; this is equivalent to the __-a__ option. ''Warning:'' __grep --binary-files=text__ might output binary garbage, which can have nasty side effects if the output is a terminal and if the terminal driver interprets some of it as commands. __-c__, __--count__ Suppress normal output; instead print a count of matching lines for each input file. With the __-v__, __--invert-match__ option (see below), count non-matching lines. __-d__ ''ACTION'', __--directories=__''ACTION'' If an input file is a directory, use ''ACTION'' to process it. By default, ''ACTION'' is __read__, which means that directories are read just as if they were ordinary files. If ''ACTION'' is __skip__, directories are silently skipped. If ''ACTION'' is __recurse__, __grep__ reads all files under each directory, recursively; this is equivalent to the __-r__ option. __-E__, __--extended-regexp__ Interpret ''PATTERN'' as an extended regular expression (see below). __-e__ ''PATTERN'', __--regexp=__''PATTERN'' Use ''PATTERN'' as the pattern; useful to protect patterns beginning with __-__. __-F__, __--fixed-strings__ Interpret ''PATTERN'' as a list of fixed strings, separated by newlines, any of which is to be matched. __-f__ ''FILE'', __--file=__''FILE'' Obtain patterns from ''FILE'', one per line. The empty file contains zero patterns, and therefore matches nothing. __-G__, __--basic-regexp__ Interpret ''PATTERN'' as a basic regular expression (see below). This is the default. __-H__, __--with-filename__ Print the filename for each match. __-h__, __--no-filename__ Suppress the prefixing of filenames on output when multiple files are searched. __--help__ Output a brief help message. __-I__ Process a binary file as if it did not contain matching data; this is equivalent to the __--binary-files=without-match__ option. __-i__, __--ignore-case__ Ignore case distinctions in both the ''PATTERN'' and the input files. __-L__, __--files-without-match__ Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match. __-l__, __--files-with-matches__ Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. __--mmap__ If possible, use the mmap(2) system call to read input, instead of the default read(2) system call. In some situations, __--mmap__ yields better performance. However, __--mmap__ can cause undefined behavior (including core dumps) if an input file shrinks while __grep__ is operating, or if an I/O error occurs. __-n__, __--line-number__ Prefix each line of output with the line number within its input file. __-q__, __--quiet__, __--silent__ Quiet; suppress normal output. The scanning will stop on the first match. Also see the __-s__ or __--no-messages__ option below. __-r__, __--recursive__ Read all files under each directory, recursively; this is equivalent to the __-d recurse__ option. __-s__, __--no-messages__ Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU __grep__, traditional __grep__ did not conform to POSIX.2 , because traditional __grep__ lacked a __-q__ option and its __-s__ option behaved like GNU __grep__'s __-q__ option. Shell scripts intended to be portable to traditional __grep__ should avoid both __-q__ and __-s__ and should redirect output to /dev/null instead. __-U__, __--binary__ 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__ overrules this guesswork, causing 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. This option has no effect on platforms other than MS-DOS and MS-Windows. __-u__, __--unix-byte-offsets__ Report Unix-style byte offsets. This switch causes __grep__ to report byte offsets as if the file were Unix-style text file, i.e. with CR characters stripped off. This will produce results identical to running __grep__ on a Unix machine. This option has no effect unless __-b__ option is also used; it has no effect on platforms other than MS-DOS and MS-Windows. __-V__, __--version__ Print the version number of __grep__ to standard error. This version number should be included in all bug reports (see below). __-v__, __--invert-match__ Invert the sense of matching, to select non-matching lines. __-w__, __--word-regexp__ Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore. __-x__, __--line-regexp__ Select only those matches that exactly match the whole line. __-y__ Obsolete synonym for __-i__. __-Z__, __--null__ Output a zero byte (the ASCII __NUL__ character) instead of the character that normally follows a file name. For example, __grep -lZ__ outputs a zero byte after each file name instead of the usual newline. This option makes the output unambiguous, even in the presence of file names containing unusual characters like newlines. This option can be used with commands like __find -print0__, __perl -0__, __sort -z__, and __xargs -0__ to process arbitrary file names, even those that contain newline characters. !!REGULAR EXPRESSIONS A regular expression is a pattern that describes a set of strings. Regular expressions are constructed analogously to arithmetic expressions, by using various operators to combine smaller expressions. __Grep__ understands two different versions of regular expression syntax: ``basic'' and ``extended.'' In GNU __grep__, there is no difference in available functionality using either syntax. In other implementations, basic regular expressions are less powerful. The following description applies to extended regular expressions; differences for basic regular expressions are summarized afterwards. The fundamental building blocks are the regular expressions that match a single character. Most characters, including all letters and digits, are regular expressions that match themselves. Any metacharacter with special meaning may be quoted by preceding it with a backslash. A list of characters enclosed by __[[__ and __]__ matches any single character in that list; if the first character of the list is the caret __^__ then it matches any character ''not'' in the list. For example, the regular expression __[[0123456789]__ matches any single digit. A range of characters may be specified by giving the first and last characters, separated by a hyphen. Finally, certain named classes of characters are predefined. Their names are self explanatory, and they are __[[:alnum:]__, __[[:alpha:]__, __[[:cntrl:]__, __[[:digit:]__, __[[:graph:]__, __[[:lower:]__, __[[:print:]__, __[[:punct:]__, __[[:space:]__, __[[:upper:]__, and __[[:xdigit:].__ For example, __[[[[:alnum:]]__ means __[[0-9A-Za-z]__, except the latter form depends upon the POSIX locale and the ASCII character encoding, whereas the former is independent of locale and character set. (Note that the brackets in these class names are part of the symbolic names, and must be included in addition to the brackets delimiting the bracket list.) Most metacharacters lose their special meaning inside lists. To include a literal __]__ place it first in the list. Similarly, to include a literal __^__ place it anywhere but first. Finally, to include a literal __-__ place it last. The period __.__ matches any single character. The symbol __w__ is a synonym for __[[[[:alnum:]]__ and __W__ is a synonym for __[[^[[:alnum]]__. The caret __^__ and the dollar sign __$__ are metacharacters that respectively match the empty string at the beginning and end of a line. The symbols ____ and ____ respectively match the empty string at the beginning and end of a word. The symbol __b__ matches the empty string at the edge of a word, and __B__ matches the empty string provided it's ''not'' at the edge of a word. A regular expression may be followed by one of several repetition operators: __?__ The preceding item is optional and matched at most once. __*__ The preceding item will be matched zero or more times. __+__ The preceding item will be matched one or more times. __{__''n''__}__ The preceding item is matched exactly ''n'' times. __{__''n''__,}__ The preceding item is matched ''n'' or more times. __{__''n''__,__''m''__}__ The preceding item is matched at least ''n'' times, but not more than ''m'' times. Two regular expressions may be concatenated; the resulting regular expression matches any string formed by concatenating two substrings that respectively match the concatenated subexpressions. Two regular expressions may be joined by the infix operator __|__; the resulting regular expression matches any string matching either subexpression. Repetition takes precedence over concatenation, which in turn takes precedence over alternation. A whole subexpression may be enclosed in parentheses to override these precedence rules. The backreference __\__''n'', where ''n'' is a single digit, matches the substring previously matched by the ''n''th parenthesized subexpression of the regular expression. In basic regular expressions the metacharacters __?__, __+__, __{__, __}__, __|__, __(__, and __)__ lose their special meaning; instead use the backslashed versions __?__, __+__, __{__, __}__, __|__, ____, and __)__. Traditional __egrep__ did not support the __{__ metacharacter, and some __egrep__ implementations support __{__ instead, so portable scripts should avoid __{__ in __egrep__ patterns and should use __[[{]__ to match a literal __{__. GNU __egrep__ attempts to support traditional usage by assuming that __{__ is not special if it would be the start of an invalid interval specification. For example, the shell command __egrep '{1'__ searches for the two-character string __{1__ instead of reporting a syntax error in the regular expression. POSIX.2 allows this behavior as an extension, but portable scripts should avoid it. !!ENVIRONMENT VARIABLES __GREP_OPTIONS__ This variable specifies default options to be placed in front of any explicit options. For example, if __GREP_OPTIONS__ is __'--binary-files=without-match --directories=skip'__, __grep__ behaves as if the two options __--binary-files=without-match__ and __--directories=skip__ had been specified before any explicit options. Option specifications are separated by whitespace. A backslash escapes the next character, so it can be used to specify an option containing whitespace or a backslash. __LC_ALL__, __LC_MESSAGES__, __LANG__ These variables specify the __LC_MESSAGES__ locale, which determines the language that __grep__ uses for messages. The locale is determined by the first of these variables that is set. American English is used if none of these environment variables are set, or if the message catalog is not installed, or if __grep__ was not compiled with national language support ( NLS ). __LC_ALL__, __LC_CTYPE__, __LANG__ These variables specify the __LC_CTYPE__ locale, which determines the type of characters, e.g., which characters are whitespace. The locale is determined by the first of these variables that is set. The POSIX locale is used if none of these environment variables are set, or if the locale catalog is not installed, or if __grep__ was not compiled with national language support ( NLS ). __POSIXLY_CORRECT__ If set, __grep__ behaves as POSIX.2 requires; otherwise, __grep__ behaves more like other GNU programs. POSIX.2 requires that options that follow file names must be treated as file names; by default, such options are permuted to the front of the operand list and are treated as options. Also, POSIX.2 requires that unrecognized options be diagnosed as ``illegal'', but since they are not really against the law the default is to diagnose them as ``invalid''. __POSIXLY_CORRECT__ also disables _____''N''___GNU_nonoption_argv_flags___, described below. _____''N''___GNU_nonoption_argv_flags___ (Here ''N'' is __grep__'s numeric process ID.) If the ''i''th character of this environment variable's value is __1__, do not consider the ''i''th operand of __grep__ 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. This behavior is available only with the GNU C library, and only when __POSIXLY_CORRECT__ is not set. !!DIAGNOSTICS Normally, exit status is 0 if matches were found, and 1 if no matches were found. (The __-v__ option inverts the sense of the exit status.) Exit status is 2 if there were syntax errors in the pattern, inaccessible input files, or other system errors. !!BUGS Email bug reports to __bug-gnu-utils@gnu.org__. Be sure to include the word ``grep'' somewhere in the ``Subject:'' field. Large repetition counts in the __{__''m''__,__''n''__}__ construct may cause grep to use lots of memory. In addition, certain other obscure regular expressions require exponential time and space, and may cause __grep__ to run out of memory. Backreferences are very slow, and may require exponential time. ----
24 pages link to
grep(1)
:
perlfunc(1)
procmail(1)
procmailex(5)
Man1g
KnowYourSysadmin
fortune(6)
bzgrep(1)
look(1)
ip(8)
procmailsc(5)
UnixWay
zegrep(1)
zfgrep(1)
zgrep(1)
procmailrc(5)
apropos(1)
MeetingTopics.2002
apt-move(8)
makeindex(1)
TextUtilities
TwoLetterCommands
BashOneLiners
ThingsToDoWithYourLinuxBox
RegularExpression
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.