grep.texi

来自「linux平台中」· TEXI 代码 · 共 1,814 行 · 第 1/5 页

TEXI
1,814
字号
@cindex changing name of standard inputDisplays input actually coming from standard input as input coming from file@var{LABEL}. This is especially useful for tools like zgrep, e.g.@command{gzip -cd foo.gz |grep --label=foo something}@item -L@itemx --files-without-match@opindex -L@opindex --files-without-match@cindex files which don't matchSuppress normal output; instead print the name of each inputfile from which no output would normally have been printed.The scanning of every file will stop on the first match.@item -a@itemx --text@opindex -a@opindex --text@cindex suppress binary data@cindex binary filesProcess a binary file as if it were text; this is equivalent to the@samp{--binary-files=text} option.@item -IProcess a binary file as if it did not contain matching data; this isequivalent to the @samp{--binary-files=without-match} option.@item -w@itemx --word-regexp@opindex -w@opindex --word-regexp@cindex matching whole wordsSelect only those lines containing matches that formwhole words.  The test is that the matching substringmust either be at the beginning of the line, or precededby a non-word constituent character.  Similarly,it must be either at the end of the line or followed bya non-word constituent character.  Word-constituentcharacters are letters, digits, and the underscore.@item -r@itemx -R@itemx --recursive@opindex -r@opindex --recursive@cindex recursive search@cindex searching directory treesFor each directory mentioned in the command line, read and process allfiles in that directory, recursively.  This is the same as the@samp{--directories=recurse} option.@item --include=@var{file_pattern}@opindex --include@cindex include files@cindex searching directory treesWhen processing directories recursively, only files matching @var{file_pattern}will be search.@item --exclude=@var{file_pattern}@opindex --exclude@cindex exclude files@cindex searching directory treesWhen processing directories recursively, skip files matching @var{file_pattern}.@item -m @var{num}@itemx --max-count=@var{num}@opindex -m@opindex --max-count@cindex max-countStop reading a file after @var{num} matching lines.  If the input isstandard input from a regular file, and @var{num} matching lines areoutput, @command{grep} ensures that the standard input is positioned tojust after the last matching line before exiting, regardless of thepresence of trailing context lines.  This enables a calling processto resume a search.  For example, the following shell script makes useof it:@examplewhile grep -m 1 PATTERNdo  echo xxxxdone < FILE@end exampleBut the following probably will not work because a pipe is not a regularfile:@example# This probably will not work.cat FILE |while grep -m 1 PATTERNdo  echo xxxxdone@end exampleWhen @command{grep} stops after NUM matching lines, it outputsany trailing context lines. Since context does not include matchinglines, @command{grep} will stop when it encounters another matching line.When the @samp{-c} or @samp{--count} option is also used,@command{grep} does not output a count greater than @var{num}.When the @samp{-v} or @samp{--invert-match} option isalso used, @command{grep} stops after outputting @var{num}non-matching lines.@item -y@opindex -y@cindex case insensitive search, obsolete optionObsolete synonym for @samp{-i}.@item -U@itemx --binary@opindex -U@opindex --binary@cindex DOS/Windows binary files@cindex binary files, DOS/WindowsTreat the file(s) as binary.  By default, under @sc{ms-dos}and MS-Windows, @command{grep} guesses the file type by lookingat the contents of the first 32kB read from the file.If @command{grep} decides the file is a text file, it strips the@code{CR} characters from the original file contents (to makeregular expressions with @code{^} and @code{$} work correctly).Specifying @samp{-U} overrules this guesswork, causing allfiles to be read and passed to the matching mechanismverbatim; if the file is a text file with @code{CR/LF} pairsat the end of each line, this will cause some regularexpressions to fail.  This option has no effect on platforms other than@sc{ms-dos} and MS-Windows.@item -u@itemx --unix-byte-offsets@opindex -u@opindex --unix-byte-offsets@cindex DOS byte offsets@cindex byte offsets, on DOS/WindowsReport Unix-style byte offsets.  This switch causes@command{grep} to report byte offsets as if the file were Unix styletext file, i.e., the byte offsets ignore the @code{CR} characters which werestripped.  This will produce results identical to running @command{grep} ona Unix machine.  This option has no effect unless @samp{-b}option is also used; it has no effect on platforms other than @sc{ms-dos} andMS-Windows.@item --mmap@opindex --mmap@cindex memory mapped inputIf possible, use the @code{mmap} system call to read input, instead ofthe default @code{read} system call.  In some situations, @samp{--mmap}yields better performance.  However, @samp{--mmap} can cause undefinedbehavior (including core dumps) if an input file shrinks while@command{grep} is operating, or if an I/O error occurs.@item -Z@itemx --null@opindex -Z@opindex --null@cindex zero-terminated file namesOutput a zero byte (the @sc{ascii} @code{NUL} character) instead of thecharacter that normally follows a file name.  For example, @samp{grep-lZ} outputs a zero byte after each file name instead of the usualnewline.  This option makes the output unambiguous, even in the presenceof file names containing unusual characters like newlines.  This optioncan be used with commands like @samp{find -print0}, @samp{perl -0},@samp{sort -z}, and @samp{xargs -0} to process arbitrary file names,even those that contain newline characters.@item -z@itemx --null-data@opindex -z@opindex --null-data@cindex zero-terminated linesTreat the input as a set of lines, each terminated by a zero byte (the@sc{ascii} @code{NUL} character) instead of a newline.  Like the @samp{-Z}or @samp{--null} option, this option can be used with commands like@samp{sort -z} to process arbitrary file names.@end tableSeveral additional options control which variant of the @command{grep}matching engine is used.  @xref{Grep Programs}.@section Environment VariablesGrep's behavior is affected by the following environment variables.A locale @code{LC_@var{foo}} is specified by examining the threeenvironment variables @env{LC_ALL}, @env{LC_@var{foo}}, and @env{LANG},in that order.  The first of these variables that is set specifies thelocale.  For example, if @env{LC_ALL} is not set, but @env{LC_MESSAGES}is set to @samp{pt_BR}, then Brazilian Portuguese is used for the@code{LC_MESSAGES} locale.  The C locale is used if none of theseenvironment variables are set, or if the locale catalog is notinstalled, or if @command{grep} was not compiled with national languagesupport (@sc{nls}).@cindex environment variables@table @env@item GREP_OPTIONS@vindex GREP_OPTIONS@cindex default options environment variableThis variable specifies default options to be placed in front of anyexplicit options.  For example, if @code{GREP_OPTIONS} is@samp{--binary-files=without-match --directories=skip}, @command{grep}behaves as if the two options @samp{--binary-files=without-match} and@samp{--directories=skip} had been specified beforeany explicit options.  Option specifications are separated bywhitespace.  A backslash escapes the next character, so it can be used tospecify an option containing whitespace or a backslash.@item GREP_COLOR@vindex GREP_COLOR@cindex highlight markersThis variable specifies the surrounding markers use to highlight the matchingtext.  The default is control ascii red.@item LC_ALL@itemx LC_COLLATE@itemx LANG@vindex LC_ALL@vindex LC_COLLATE@vindex LANG@cindex character type@cindex national language support@cindex NLSThese variables specify the @code{LC_COLLATE} locale, which determinesthe collating sequence used to interpret range expressions like@samp{[a-z]}.@item LC_ALL@itemx LC_CTYPE@itemx LANG@vindex LC_ALL@vindex LC_CTYPE@vindex LANG@cindex character type@cindex national language support@cindex NLSThese variables specify the @code{LC_CTYPE} locale, which determines thetype of characters, e.g., which characters are whitespace.@item LC_ALL@itemx LC_MESSAGES@itemx LANG@vindex LC_ALL@vindex LC_MESSAGES@vindex LANG@cindex language of messages@cindex message language@cindex national language support@cindex NLS@cindex translation of message languageThese variables specify the @code{LC_MESSAGES} locale, which determinesthe language that @command{grep} uses for messages.  The default Clocale uses American English messages.@item POSIXLY_CORRECT@vindex POSIXLY_CORRECTIf set, @command{grep} behaves as @sc{posix.2} requires; otherwise,@command{grep} behaves more like other @sc{gnu} programs.  @sc{posix.2}requires that options thatfollow file names must be treated as file names; by default, suchoptions are permuted to the front of the operand list and are treated asoptions.  Also, @sc{posix.2} requires that unrecognized options bediagnosed as``illegal'', but since they are not really against the law the defaultis to diagnose them as ``invalid''.  @code{POSIXLY_CORRECT} alsodisables @code{_@var{N}_GNU_nonoption_argv_flags_}, described below.@item _@var{N}_GNU_nonoption_argv_flags_@vindex _@var{N}_GNU_nonoption_argv_flags_(Here @code{@var{N}} is @command{grep}'s numeric process ID.)  If the@var{i}th character of this environment variable's value is @samp{1}, donot consider the @var{i}th operand of @command{grep} to be an option, even ifit appears to be one.  A shell can put this variable in the environmentfor each command it runs, specifying which operands are the results offile name wildcard expansion and therefore should not be treated asoptions.  This behavior is available only with the @sc{gnu} C library, andonly when @code{POSIXLY_CORRECT} is not set.@end table@node Diagnostics@chapter DiagnosticsNormally, exit status is 0 if selected lines are found and 1 otherwise.But the exit status is 2 if an error occurred, unless the @option{-q} or@option{--quiet} or @option{--silent} option is used and a selected lineis found.@node Grep Programs@chapter @command{grep} programs@command{grep} searches the named input files (or standard input if nofiles are named, or the file name @file{-} is given) for lines containinga match to the given pattern.  By default, @command{grep} prints thematching lines.  There are four major variants of @command{grep},controlled by the following options.@table @samp@item -G@itemx --basic-regexp@opindex -G@opindex --basic-regexp@cindex matching basic regular expressionsInterpret the pattern as a basic regular expression.  This is the default.@item -E@itemx --extended-regexp@opindex -E@opindex --extended-regexp@cindex matching extended regular expressionsInterpret the pattern as an extended regular expression.@item -F@itemx --fixed-strings@opindex -F@opindex --fixed-strings@cindex matching fixed stringsInterpret the pattern as a list of fixed strings, separatedby newlines, any of which is to be matched.@item -P@itemx --perl-regexp@opindex -P@opindex --perl-regexp@cindex matching Perl regular expressionsInterpret the pattern as a Perl regular expression.@end tableIn addition, two variant programs @sc{egrep} and @sc{fgrep} are available.@sc{egrep} is the same as @samp{grep -E}.  @sc{fgrep} is thesame as @samp{grep -F}.@node Regular Expressions@chapter Regular Expressions@cindex regular expressionsA @dfn{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.@command{grep} understands two different versions of regular expressionsyntax: ``basic''(BRE) and ``extended''(ERE).  In @sc{gnu} @command{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 matcha single character.  Most characters, including all letters and digits,are regular expressions that match themselves.  Any metacharacterwith special meaning may be quoted by preceding it with a backslash.A regular expression may be followed by one of severalrepetition operators:@table @samp@item .@opindex .

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?