📄 grep.1
字号:
.BR \-v ", " \-\^\-invert-matchInvert the sense of matching, to select non-matching lines..TP.BR \-w ", " \-\^\-word-regexpSelect only those lines containing matches that form whole words.The test is that the matching substring must either be at thebeginning of the line, or preceded by a non-word constituentcharacter. Similarly, it must be either at the end of the lineor followed by a non-word constituent character. Word-constituentcharacters are letters, digits, and the underscore..TP.BR \-x ", " \-\^\-line-regexpSelect only those matches that exactly match the whole line..TP.B \-yObsolete synonym for.BR \-i ..TP.BR \-Z ", " \-\^\-nullOutput a zero byte (the \s-1ASCII\s0.B NULcharacter) instead of the character that normally follows a file name.For example,.B "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 filenames containing unusual characters like newlines. This option can beused with commands like.BR "find \-print0" ,.BR "perl \-0" ,.BR "sort \-z" ,and.B "xargs \-0"to process arbitrary file names,even those that contain newline characters..SH "REGULAR EXPRESSIONS".PPA regular expression is a pattern that describes a set of strings.Regular expressions are constructed analogously to arithmeticexpressions, by using various operators to combine smaller expressions..PP.B Grepunderstands two different versions of regular expression syntax:\*(lqbasic\*(rq and \*(lqextended.\*(rq In.RB "\s-1GNU\s0\ " 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..PPThe fundamental building blocks are the regular expressions that matcha single character. Most characters, including all letters and digits,are regular expressions that match themselves. Any metacharacter withspecial meaning may be quoted by preceding it with a backslash..PPA.I "bracket expression"is a list of characters enclosed by.B [and.BR ] .It matches any singlecharacter in that list; if the first character of the listis the caret.B ^then it matches any character.I notin the list.For example, the regular expression.B [0123456789]matches any single digit..PPWithin a bracket expression, a.I "range expression"consists of two characters separated by a hyphen.It matches any single character that sorts between the two characters,inclusive, using the locale's collating sequence and character set.For example, in the default C locale,.B [a\-d]is equivalent to.BR [abcd] .Many locales sort characters in dictionary order, and in these locales.B [a\-d]is typically not equivalent to.BR [abcd] ;it might be equivalent to.BR [aBbCcDd] ,for example.To obtain the traditional interpretation of bracket expressions,you can use the C locale by setting the.B LC_ALLenvironment variable to the value.BR C ..PPFinally, certain named classes of characters are predefined withinbracket expressions, as follows.Their names are self explanatory, and they are.BR [:alnum:] ,.BR [:alpha:] ,.BR [:cntrl:] ,.BR [:digit:] ,.BR [:graph:] ,.BR [:lower:] ,.BR [:print:] ,.BR [:punct:] ,.BR [:space:] ,.BR [:upper:] ,and.BR [:xdigit:].For example,.B [[:alnum:]]means.BR [0\-9A\-Za\-z] ,except the latter form depends upon the C locale and the\s-1ASCII\s0 character encoding, whereas the former is independentof locale and character set.(Note that the brackets in these class names are part of the symbolicnames, and must be included in addition to the brackets delimitingthe bracket list.) Most metacharacters lose their special meaninginside lists. To include a literal.B ]place it first in the list. Similarly, to include a literal.B ^place it anywhere but first. Finally, to include a literal.B \-place it last..PPThe period.B .matches any single character.The symbol.B \ewis a synonym for.B [[:alnum:]]and.B \eWis a synonym for.BR [^[:alnum]] ..PPThe caret.B ^and the dollar sign.B $are metacharacters that respectively match the empty string at thebeginning and end of a line.The symbols.B \e<and.B \e>respectively match the empty string at the beginning and end of a word.The symbol.B \ebmatches the empty string at the edge of a word,and.B \eBmatches the empty string provided it's.I notat the edge of a word..PPA regular expression may be followed by one of several repetition operators:.PD 0.TP.B ?The preceding item is optional and matched at most once..TP.B *The preceding item will be matched zero or more times..TP.B +The preceding item will be matched one or more times..TP.BI { n }The preceding item is matched exactly.I ntimes..TP.BI { n ,}The preceding item is matched.I nor more times..TP.BI { n , m }The preceding item is matched at least.I ntimes, but not more than.I mtimes..PD.PPTwo regular expressions may be concatenated; the resultingregular expression matches any string formed by concatenatingtwo substrings that respectively match the concatenatedsubexpressions..PPTwo regular expressions may be joined by the infix operator.BR | ;the resulting regular expression matches any string matchingeither subexpression..PPRepetition takes precedence over concatenation, which in turntakes precedence over alternation. A whole subexpression may beenclosed in parentheses to override these precedence rules..PPThe backreference.BI \e n\c\&, where.I nis a single digit, matches the substringpreviously matched by the.IR n thparenthesized subexpression of the regular expression..PPIn basic regular expressions the metacharacters.BR ? ,.BR + ,.BR { ,.BR | ,.BR ( ,and.BR )lose their special meaning; instead use the backslashedversions.BR \e? ,.BR \e+ ,.BR \e{ ,.BR \e| ,.BR \e( ,and.BR \e) ..PPTraditional.B egrepdid not support the.B {metacharacter, and some.B egrepimplementations support.B \e{instead, so portable scripts should avoid.B {in.B egreppatterns and should use.B [{]to match a literal.BR { ..PP\s-1GNU\s0.B egrepattempts to support traditional usage by assuming that.B {is not special if it would be the start of an invalid intervalspecification. For example, the shell command.B "egrep '{1'"searches for the two-character string.B {1instead of reporting a syntax error in the regular expression.\s-1POSIX.2\s0 allows this behavior as an extension, but portable scriptsshould avoid it..SH "ENVIRONMENT VARIABLES"Grep's behavior is affected by the following environment variables..PPA locale.BI LC_ foois specified by examining the three environment variables.BR LC_ALL ,.BR LC_\fIfoo\fP ,.BR LANG ,in that order.The first of these variables that is set specifies the locale.For example, if.B LC_ALLis not set, but.B LC_MESSAGESis set to.BR pt_BR ,then Brazilian Portuguese is used for the.B LC_MESSAGESlocale.The C locale is used if none of these environment variables are set,or if the locale catalog is not installed, or if.B grepwas not compiled with national language support (\s-1NLS\s0)..TP.B GREP_OPTIONSThis variable specifies default options to be placed in front of anyexplicit options. For example, if.B GREP_OPTIONSis.BR "'\-\^\-binary-files=without-match \-\^\-directories=skip'" ,.B grepbehaves as if the two options.B \-\^\-binary-files=without-matchand.B \-\^\-directories=skiphad 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..TP.B GREP_COLORSpecifies the marker for highlighting..TP\fBLC_ALL\fP, \fBLC_COLLATE\fP, \fBLANG\fPThese variables specify the.B LC_COLLATElocale, which determines the collating sequence used to interpretrange expressions like.BR [a\-z] ..TP\fBLC_ALL\fP, \fBLC_CTYPE\fP, \fBLANG\fPThese variables specify the.B LC_CTYPElocale, which determines the type of characters, e.g., whichcharacters are whitespace..TP\fBLC_ALL\fP, \fBLC_MESSAGES\fP, \fBLANG\fPThese variables specify the.B LC_MESSAGESlocale, which determines the language that.B grepuses for messages.The default C locale uses American English messages..TP.B POSIXLY_CORRECTIf set,.B grepbehaves as \s-1POSIX.2\s0 requires; otherwise,.B grepbehaves more like other \s-1GNU\s0 programs.\s-1POSIX.2\s0 requires that options that follow file names must betreated as file names; by default, such options are permuted to thefront of the operand list and are treated as options.Also, \s-1POSIX.2\s0 requires that unrecognized options be diagnosed as\*(lqillegal\*(rq, but since they are not really against the law the defaultis to diagnose them as \*(lqinvalid\*(rq..B POSIXLY_CORRECTalso disables \fB_\fP\fIN\fP\fB_GNU_nonoption_argv_flags_\fP,described below..TP\fB_\fP\fIN\fP\fB_GNU_nonoption_argv_flags_\fP(Here.I Nis.BR grep 'snumeric process ID.) If the.IR i thcharacter of this environment variable's value is.BR 1 ,do not consider the.IR i thoperand of.B grepto 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 wildcardexpansion and therefore should not be treated as options.This behavior is available only with the \s-1GNU\s0 C library, and onlywhen.B POSIXLY_CORRECTis not set..SH DIAGNOSTICS.PPNormally, exit status is 0 if selected lines are found and 1 otherwise.But the exit status is 2 if an error occurred, unless the.B \-qor.B \-\^\-quietor.B \-\^\-silentoption is used and a selected line is found..SH BUGS.PPEmail bug reports to.BR bug-gnu-utils@gnu.org .Be sure to include the word \*(lqgrep\*(rq somewhere in the\*(lqSubject:\*(rq field..PPLarge repetition counts in the.BI { n , m }construct may cause grep to use lots of memory.In addition,certain other obscure regular expressions require exponential timeand space, and may cause.B grepto run out of memory..PPBackreferences are very slow, and may require exponential time..\" Work around problems with some troff -man implementations..br
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -