grep.texi
来自「linux平台中」· TEXI 代码 · 共 1,814 行 · 第 1/5 页
TEXI
1,814 行
@cindex dot@cindex periodThe period @samp{.} matches any single character.@item ?@opindex ?@cindex question mark@cindex match sub-expression at most onceThe preceding item is optional and will be matched at most once.@item *@opindex *@cindex asterisk@cindex match sub-expression zero or more timesThe preceding item will be matched zero or more times.@item +@opindex +@cindex plus signThe preceding item will be matched one or more times.@item @{@var{n}@}@opindex @{n@}@cindex braces, one argument@cindex match sub-expression n timesThe preceding item is matched exactly @var{n} times.@item @{@var{n},@}@opindex @{n,@}@cindex braces, second argument omitted@cindex match sub-expression n or more timesThe preceding item is matched n or more times.@item @{@var{n},@var{m}@}@opindex @{n,m@}@cindex braces, two argumentsThe preceding item is matched at least @var{n} times, but not more than@var{m} times.@end tableTwo regular expressions may be concatenated; the resulting regularexpression matches any string formed by concatenating two substringsthat respectively match the concatenated subexpressions.Two regular expressions may be joined by the infix operator @samp{|}; theresulting regular expression matches any string matching either subexpression.Repetition takes precedence over concatenation, which in turntakes precedence over alternation. A whole subexpression may beenclosed in parentheses to override these precedence rules.@section Character Class@cindex bracket expression@cindex character classA @dfn{bracket expression} is a list of characters enclosed by @samp{[} and@samp{]}. It matches any single character in that list; if the firstcharacter of the list is the caret @samp{^}, then it matches any character@strong{not} in the list. For example, the regular expression@samp{[0123456789]} matches any single digit.@cindex range expressionWithin a bracket expression, a @dfn{range expression} consists of twocharacters separated by a hyphen. It matches any single character thatsorts between the two characters, inclusive, using the locale'scollating sequence and character set. For example, in the default Clocale, @samp{[a-d]} is equivalent to @samp{[abcd]}. Many locales sortcharacters in dictionary order, and in these locales @samp{[a-d]} istypically not equivalent to @samp{[abcd]}; it might be equivalent to@samp{[aBbCcDd]}, for example. To obtain the traditional interpretationof bracket expressions, you can use the C locale by setting the@env{LC_ALL} environment variable to the value @samp{C}.Finally, certain named classes of characters are predefined withinbracket expressions, as follows.Their interpretation depends on the @code{LC_CTYPE} locale; theinterpretation below is that of the C locale, which is the defaultif no @code{LC_CTYPE} locale is specified.@cindex classes of characters@cindex character classes@table @samp@item [:alnum:]@opindex alnum@cindex alphanumeric charactersAlphanumeric characters:@samp{[:alpha:]} and @samp{[:digit:]}.@item [:alpha:]@opindex alpha@cindex alphabetic charactersAlphabetic characters:@samp{[:lower:]} and @samp{[:upper:]}.@item [:blank:]@opindex blank@cindex blank charactersBlank characters:space and tab.@item [:cntrl:]@opindex cntrl@cindex control charactersControl characters. In @sc{ascii}, these characters have octal codes 000through 037, and 177 (@code{DEL}). In other character sets, these arethe equivalent characters, if any.@item [:digit:]@opindex digit@cindex digit characters@cindex numeric charactersDigits: @code{0 1 2 3 4 5 6 7 8 9}.@item [:graph:]@opindex graph@cindex graphic charactersGraphical characters:@samp{[:alnum:]} and @samp{[:punct:]}.@item [:lower:]@opindex lower@cindex lower-case lettersLower-case letters:@code{a b c d e f g h i j k l m n o p q r s t u v w x y z}.@item [:print:]@opindex print@cindex printable charactersPrintable characters:@samp{[:alnum:]}, @samp{[:punct:]}, and space.@item [:punct:]@opindex punct@cindex punctuation charactersPunctuation characters:@code{!@: " # $ % & ' ( ) * + , - .@: / : ; < = > ?@: @@ [ \ ] ^ _ ` @{ | @} ~}.@item [:space:]@opindex space@cindex space characters@cindex whitespace charactersSpace characters:tab, newline, vertical tab, form feed, carriage return, and space.@item [:upper:]@opindex upper@cindex upper-case lettersUpper-case letters:@code{A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}.@item [:xdigit:]@opindex xdigit@cindex xdigit class@cindex hexadecimal digitsHexadecimal digits:@code{0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f}.@end tableFor example, @samp{[[:alnum:]]} means @samp{[0-9A-Za-z]}, except the latterdepends upon the C locale and the @sc{ascii} characterencoding, whereas the former is independent of locale and character set.(Note that the brackets in these class names arepart of the symbolic names, and must be included in addition tothe brackets delimiting the bracket list.)Most metacharacters lose their special meaning inside lists.@table @samp@item ]ends the list if it's not the first list item. So, if you want to makethe @samp{]} character a list item, you must put it first.@item [.represents the open collating symbol.@item .]represents the close collating symbol.@item [=represents the open equivalence class.@item =]represents the close equivalence class.@item [:represents the open character class followed by a valid character class name.@item :]represents the close character class followed by a valid character class name.@item -represents the range if it's not first or last in a list or the ending pointof a range.@item ^represents the characters not in the list. If you want to make the @samp{^}character a list item, place it anywhere but first.@end table@section Backslash Character@cindex backslashThe @samp{\} when followed by certain ordinary characters take a specialmeaning :@table @samp@item @samp{\b}Match the empty string at the edge of a word.@item @samp{\B}Match the empty string provided it's not at the edge of a word.@item @samp{\<}Match the empty string at the beginning of word.@item @samp{\>}Match the empty string at the end of word.@item @samp{\w}Match word constituent, it is a synonym for @samp{[[:alnum:]]}.@item @samp{\W}Match non word constituent, it is a synonym for @samp{[^[:alnum:]]}.@end tableFor example , @samp{\brat\b} matches the separate word @samp{rat},@samp{c\Brat\Be} matches @samp{crate}, but @samp{dirty \Brat} doesn'tmatch @samp{dirty rat}.@section Anchoring@cindex anchoringThe caret @samp{^} and the dollar sign @samp{$} are metacharacters thatrespectively match the empty string at the beginning and end of a line.@section Back-reference@cindex back-referenceThe back-reference @samp{\@var{n}}, where @var{n} is a single digit, matchesthe substring previously matched by the @var{n}th parenthesized subexpressionof the regular expression. For example, @samp{(a)\1} matches @samp{aa}.When use with alternation if the group does not participate in the match, thenthe back-reference makes the whole match fail. For example, @samp{a(.)|b\1}will not match @samp{ba}. When multiple regular expressions are given with@samp{-e} or from a file @samp{-f file}, the back-referecences are local toeach expression.@section Basic vs Extended@cindex basic regular expressionsIn basic regular expressions the metacharacters @samp{?}, @samp{+},@samp{@{}, @samp{|}, @samp{(}, and @samp{)} lose their special meaning;instead use the backslashed versions @samp{\?}, @samp{\+}, @samp{\@{},@samp{\|}, @samp{\(}, and @samp{\)}.@cindex interval specificationsTraditional @command{egrep} did not support the @samp{@{} metacharacter,and some @command{egrep} implementations support @samp{\@{} instead, soportable scripts should avoid @samp{@{} in @samp{egrep} patterns andshould use @samp{[@{]} to match a literal @samp{@{}.@sc{gnu} @command{egrep} attempts to support traditional usage byassuming that @samp{@{} is not special if it would be the start of aninvalid interval specification. For example, the shell command@samp{egrep '@{1'} searches for the two-character string @samp{@{1}instead of reporting a syntax error in the regular expression.@sc{posix.2} allows this behavior as an extension, but portable scriptsshould avoid it.@node Usage@chapter Usage@cindex Usage, examplesHere is an example shell command that invokes @sc{gnu} @command{grep}:@examplegrep -i 'hello.*world' menu.h main.c@end example@noindentThis lists all lines in the files @file{menu.h} and @file{main.c} thatcontain the string @samp{hello} followed by the string @samp{world};this is because @samp{.*} matches zero or more characters within a line.@xref{Regular Expressions}. The @samp{-i} option causes @command{grep}to ignore case, causing it to match the line @samp{Hello, world!}, whichit would not otherwise match. @xref{Invoking}, for more details abouthow to invoke @command{grep}.@cindex Using @command{grep}, Q&A@cindex FAQ about @command{grep} usageHere are some common questions and answers about @command{grep} usage.@enumerate@itemHow can I list just the names of matching files?@examplegrep -l 'main' *.c@end example@noindentlists the names of all C files in the current directory whose contentsmention @samp{main}.@itemHow do I search directories recursively?@examplegrep -r 'hello' /home/gigi@end example@noindentsearches for @samp{hello} in all files under the directory@file{/home/gigi}. For more control of which files are searched, use@command{find}, @command{grep} and @command{xargs}. For example,the following command searches only C files:@smallexamplefind /home/gigi -name '*.c' -print | xargs grep 'hello' /dev/null@end smallexampleThis differs from the command:@examplegrep -r 'hello' *.c@end examplewhich merely looks for @samp{hello} in all files in the currentdirectory whose names end in @samp{.c}. Here the @option{-r} isprobably unnecessary, as recursion occurs only in the unlikely eventthat one of @samp{.c} files is a directory.@itemWhat if a pattern has a leading @samp{-}?@examplegrep -e '--cut here--' *@end example@noindentsearches for all lines matching @samp{--cut here--}. Without @samp{-e},@command{grep} would attempt to parse @samp{--cut here--} as a list ofoptions.@itemSuppose I want to search for a whole word, not a part of a word?@examplegrep -w 'hello' *@end example@noindentsearches only for instances of @samp{hello} that are entire words; itdoes not match @samp{Othello}. For more control, use @samp{\<} and@samp{\>} to match the start and end of words. For example:@example
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?