命令行大全

egrep

瑞兹 发表于 2021-01-18 00:43浏览次数:

在类似Unix的操作系统上,egrep命令使用扩展​​的正则表达式执行匹配以搜索文本模式。运行egrep等效于使用-E选项运行grep。

查看英文版

目录:

1 egrep 运行系统环境

2 egrep 语法

3 egrep 例子

egrep 运行系统环境

Unix&Linux

egrep 语法

egrep [options] PATTERN [FILE...]

选件

-A NUM,-- after-context = NUM 匹配行后打印NUM行尾随上下文。在连续的匹配组之间放置包含-的行。
-a,-- text 像对待文本一样处理二进制文件;这等效于--binary-files = text选项。
-B NUM,-- before-context = NUM 在匹配行之前打印前导上下文的NUM行。在连续的匹配组之间放置包含-的行。
-C NUM,-- context = NUM 打印NUM行输出上下文。在连续的匹配组之间放置包含-的行。
-b,-- byte-offset 在输出的每一行之前打印输入文件中的字节 偏移量。
--binary-files =TYPE 如果文件的前几个字节指示该文件包含二进制数据,则假定该文件的类型为TYPE。默认情况下,TYPE为二进制,并且grep通常会输出一条单行消息,指出二进制文件匹配,如果不匹配,则不输出消息。如果TYPE为不匹配,则grep假定二进制文件不匹配;否则,grep假定二进制文件不匹配。这等效于-I选项。如果TYPE是text,则grep将二进制文件视为文本;这等效于-a选项。警告:grep --binary-files = text可能会输出二进制垃圾,如果输出是终端并且终端驱动程序将其中一些解释为命令,则二进制垃圾可能会带来讨厌的副作用。
--colour [ WHEN ],-- color [ WHEN ] 用在GREP_COLOR 环境变量中找到的标记将匹配的字符串括起来什么时候可能是“从不”,“总是”或“自动
-c,-- count 抑制正常输出;而是为每个输入文件打印匹配行数。使用-v,-- invert-match选项(请参见下文),计算不匹配的行。
-D ACTION,-- devices = ACTION 如果输入文件是设备,FIFO或套接字,请使用ACTION进行处理。默认情况下,将读取ACTION,这意味着读取设备就像读取普通文件一样。如果ACTION被跳过,设备将被静默跳过。
-d ACTION,-- directories = ACTION 如果输入文件是目录,请使用ACTION进行处理。默认情况下,将读取ACTION,这意味着目录的读取就像它们是普通文件一样。如果ACTION被跳过,则目录被静默跳过。如果ACTION递归grep的读取每个目录下的所有文件,递归; 这等效于-r选项。
-e PATTERN,-- regexp =PATTERN 使用PATTERN作为模式;保护以“ - ”开头的模式很有用。
-F--fixed-strings PATTERN解释为固定字符串列表,用换行符分隔,可以匹配。
-P,-- perl-regexp PATTERN解释为Perl正则表达式。
-f FILE,-- file = FILE FILE获取模式,每行一个。空文件包含零个模式,因此不匹配。
-G,-- basic-regexp PATTERN解释为基本正则表达式(请参见下文)。这是默认值。
-H,-- with-filename 打印每个匹配项的文件名。
-h,-- no-filename 搜索多个文件时,禁止在输出中使用文件名前缀。
--help 输出简短的帮助消息。
--I 处理二进制文件,就好像它不包含匹配的数据一样;这等效于--binary-files = without-match选项。
-i,-- ignore-case 忽略PATTERN和输入文件中的大小写区别。
-L,-- files-without-match 抑制正常输出;而是打印每个输入文件的名称,通常不会从该文件中打印输出。扫描将在第一个匹配项时停止。
-l,-- files-with-matches 抑制正常输出;而是打印通常会从中打印输出的每个输入文件的名称。扫描将在第一个匹配项时停止。
-m NUM,-- max-count = NUM NUM条匹配的行之后停止读取文件。如果输入是来自常规文件的标准输入,并且输出NUM条匹配行,则grep确保将标准输入定位在退出之前的最后一条匹配行之后,而不管尾随上下文行的存在。这使呼叫过程可以恢复搜索。当grepNUM条匹配行之后停止时,它将输出任何尾随上下文行。当还使用-c--count选项时,grep不会输出大于NUM的计数。当-v--invert-match还使用了option,grep在输出NUM条不匹配的行后停止。
--mmap 如果可能,请使用mmap系统调用读取输入,而不是默认的读取系统调用。在某些情况下,-- mmap会产生更好的性能。但是,如果在grep运行时输入文件缩小,或者发生I / O错误,则--mmap可能导致未定义的行为(包括core dumps)。
-n,-- line-number 在输出的每一行之前在其输入文件中添加行号。
-o,-- only-matching 仅显示匹配PATTERN的匹配线部分。
--label =LABEL 将实际来自标准输入的输入显示为来自文件LABEL的输入。这对于zgrep之类的工具尤其有用,例如gzip -cd foo.gz | grep -H --label = foo
--line-buffered 使用行缓冲。这可能会导致性能下降。
-q,-- quiet,-- silent 安静; 不要在标准输出中写任何东西。如果发现任何匹配项,即使检测到错误,也以零状态立即退出。另请参见-s--no-messages选项。
-R-r,--recursive 递归读取每个目录下的所有文件;这等效于-d recurse选项。修改者:--

include = PATTERN

仅在目录中搜索与PATTERN匹配的文件,然后递归。

--exclude = PATTERN

目录中的递归会跳过与PATTERN匹配的文件。
-s--no-messages 禁止显示有关不存在或不可读文件的错误消息。可移植性说明:与GNU grep不同,传统grep不符合POSIX .2,因为传统grep缺少-q选项,并且其-s选项的行为类似于GNU grep的-q选项。打算移植到传统grep的Shell 脚本应避免同时使用-q-s,而应将输出重定向到/ dev / null
-U,-- binary 将文件视为二进制文件。默认情况下,在MS-DOS和MS Windows下,grep通过查看从文件读取的前32KB的内容来猜测文件类型。如果grep认为文件是文本文件,它将从原始文件内容中删除CR 字符(以使带有^$的正则表达式正确工作)。指定-U会否决此猜测,导致所有文件都被逐字读取并传递给匹配机制;如果文件是带有CR / LF的文本文件在每行末尾使用双对,这将导致某些正则表达式失败。此选项对MS-DOS和MS-Windows以外的平台无效。
-u,-- unix-byte-offsets 报告Unix样式的字节偏移量。此开关使grep报告字节偏移,就好像该文件是Unix样式的文本文件一样,即,去除了CR字符。这将产生与在Unix机器上运行grep相同的结果。除非也使用-b选项,否则该选项无效。它对除MS-DOS和MS-Windows以外的平台没有影响。
-V,--version grep的版本号打印为标准错误。此版本号应包含在所有错误报告中(请参见下文)。
-v,--inver-match 反转匹配感,以选择不匹配的行。
-w,-- word-regexp 仅选择包含构成整个单词的匹配项的行。测试是匹配的子字符串必须在该行的开头,或者必须在非单词组成字符之前。同样,它必须在行的末尾,或后接非单词组成字符。单词构成字符是字母,数字和下划线。
-x,-- line-regexp 仅选择与整行完全匹配的那些匹配项。
-y -i的过时同义词。
-Z,-- null 输出零字节(ASCII NULL字符),而不是通常在文件名后的字符。例如,grep -lZ在每个文件名之后输出一个零字节,而不是通常的换行符。即使存在包含诸如换行符之类的不寻常字符的文件名,此选项也可以使输出明确。此选项可与find -print0perl -0sort -zxargs -0等命令一起使用,以处理任意文件名,即使是包含换行符的文件名。

常用表达

正则表达式是描述一组字符串的模式。正则表达式是类似构造的算术表达式,通过使用不同的运营商更小的表达式组合。

Grep理解正则表达式语法的两种不同版本:“基本”和“扩展”。在GNU grep中,使用这两种语法在可用功能上没有区别。在其他实现中,基本正则表达式的功能较弱。以下描述适用于在egrep中使用的扩展正则表达式;随后总结基本正则表达式的差异。

基本构件是与单个字符匹配的正则表达式。大多数字符(包括所有字母和数字)都是匹配自己的正则表达式。具有特殊含义的任何元字符都可以在其前面加上反斜杠来引用。

方括号表达式是由[]括起来的字符的列表。它匹配该列表中的任何单个字符;如果列表的第一个字符是插入号^,则它匹配列表中未包含的任何字符。例如,正则表达式[0123456789]与任何一位数字匹配。

在方括号表达式中,范围表达式由两个字符组成,两个字符之间用连字符(-)隔开。它使用语言环境的整理顺序和字符集匹配在两个字符(包括两个字符)之间排序的任何单个字符。例如,在默认的C语言环境中,[ad]等效于[abcd]。许多语言环境都按字典顺序对字符进行排序,在这些语言环境中,[ad]通常不等同于[abcd];例如,它可能等效于[aBbCcDd]。要获取括号表达式的传统解释,可以通过将LC_ALL环境变量设置为值C来使用C语言环境。

最后,在括号表达式中预定义了某些命名的字符类,如下所示。它们的名称不言自明,分别是[:alnum:][:alpha:][:cntrl:][:digit:][:graph:][:lower:][:print:][:punct:][:space:][:upper:][:xdigit:]。例如,[[:alnum:]]表示[0-9A-Za-z],除了后者的形式取决于C语言环境和ASCII字符编码,而前者则独立于语言环境和字符集。(请注意,这些类名称中的括号是符号名称的一部分,并且除了界定括号列表的括号外,还必须包括这些括号。)大多数元字符在列表中都失去了其特殊的含义。要包含文字],请将其放在列表的第一位。同样,要包含文字^,请将其放在首位以外的任何位置。最后,要包含文字,请放在最后。

期间匹配任何单个字符。符号\ w[[:alnum:]]的同义词,\ W[^ [:alnum]]的同义词。

脱字号^和美元符号(“ $ ”)是分别与行首和结尾处的空字符串匹配的元字符。符号\ <\>分别与单词开头和结尾的空字符串匹配。符号\ b匹配单词边缘的空字符串,\ B匹配单词不在单词边缘的空字符串。

正则表达式后可以跟几个重复运算符之一:

上一项是可选的,最多匹配一次。
* 前一项将被匹配零次或多次。
+ 前一项将被匹配一次或多次。
} 上一项完全匹配n次。
,} 前一项匹配n次或多次。
} 前一项至少匹配n次,但不超过m次。

两个正则表达式可以串联; 生成的正则表达式与通过串联两个分别与串联的子表达式匹配的子字符串形成的字符串匹配。

infix运算符可以将两个正则表达式连接起来; 生成的正则表达式与匹配任一子表达式的任何字符串匹配。

重复优先于串联,反过来优先于交替。整个子表达式可以用括号括起来,以覆盖这些优先级规则。

向后引用n,其中n是一个数字,与先前由则表达式的第n个括号化的子表达式匹配的子字符串匹配。

在基本正则表达式中,元字符+{失去特殊含义;而是使用反斜杠版本\?\ +\ {\ | \(\)

传统的egrep不支持{元字符,而某些egrep实现支持\ {,因此,可移植脚本应避免使用egrep模式中的{,并应使用[{]来匹配文字{

GNU egrep尝试通过假设{如果是无效间隔说明的开始不特殊,来支持传统用法。例如,shell命令egrep'{1'搜索两个字符的字符串{1,而不是在正则表达式中报告语法错误。POSIX.2允许将此行为作为扩展,但可移植脚本应避免这种情况。

环境变量

Grep的行为受以下环境变量影响:

通过依次检查三个环境变量LC_ALLLC_fooLANG来指定语言环境LC_foo。这些设置的第一个变量指定语言环境。例如,如果未设置LC_ALL,但LC_MESSAGES设置为pt_BR,则将巴西葡萄牙语用于LC_MESSAGES语言环境。如果未设置这些环境变量,或者未安装语言环境目录,或者未使用本国语言支持(NLS)编译grep,则使用C语言环境。

GREP_OPTIONS

此变量指定将默认选项放置在任何显式选项的前面。例如,如果GREP_OPTIONS为' --binary-files = without-match --directories = skip ',则grep的行为就像--binary-files = without-match--directories = skip这两个选项在任何显式选项。选项规格由空格分隔。反斜杠转义下一个字符,因此可用于指定包含空格或反斜杠的选项。

GREP_COLOR

指定突出显示的标记。

LC_ALL,LC_COLLATE,LANG

这些变量指定LC_COLLATE语言环境,该语言环境确定用于解释范围表达式(例如[az])的整理顺序。

LC_ALL,LC_CTYPE,LANG

这些变量指定LC_CTYPE语言环境,该语言环境确定字符的类型,例如哪些字符为空格。

LC_ALL,LC_MESSAGES,LANG

这些变量指定LC_MESSAGES语言环境,该语言环境确定grep用于消息的语言。默认的C语言环境使用美国英语消息。

POSIXLY_CORRECT

如果设置,则grep表现为POSIX.2要求;否则,grep的行为将更类似于其他GNU程序。POSIX.2要求文件名后面的选项必须被视为文件名。默认情况下,此类选项被排列在操作数列表的最前面,并被视为选项。同样,POSIX.2要求将无法识别的选项诊断为“非法”,但是由于它们并非真正违法,因此默认情况下将其诊断为“无效”。POSIXLY_CORRECT还禁用_N_GNU_nonoption_argv_flags_,如下所述。

_N_GNU_nonoption_argv_flags_

(此处N是grep的数字进程ID。)如果此环境变量的值的第i个字符为1,则即使该grep的i个操作数看起来是一个,也不要将其视为选项。Shell可以为它运行的每个命令将此变量放入环境中,并指定哪些操作数是文件名通配符扩展的结果,因此不应将其视为选项。仅对于GNU C库,并且仅当未设置POSIXLY_CORRECT时,此行为才可用。

egrep [options] PATTERN [FILE...]

Options

-A NUM--after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing -- between contiguous groups of matches.
-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. Places a line containing -- between contiguous groups of matches.
-C NUM--context=NUM Print NUM lines of output context. Places a line containing -- between contiguous groups of matches.
-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.
--colour[=WHEN], --color[=WHEN] Surround the matching string with the marker find in GREP_COLOR environment variableWHEN may be 'never', 'always', or 'auto'
-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--devices=ACTION If an input file is a device, FIFO or socket, use ACTION to process it. By default, ACTION is read, which means that devices are read just as if they were ordinary files. If ACTION is skip, devices are silently skipped.
-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 recursegrep reads all files under each directory, recursively; this is equivalent to the -r option.
-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, that may be matched.
-P--perl-regexp Interpret PATTERN as a Perl regular expression.
-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.
-m NUM--max-count=NUM Stop reading a file after NUM matching lines. If the input is standard input from a regular file, and NUM matching lines are output, grep ensures that the standard input is positioned to just after the last matching line before exiting, regardless of the presence of trailing context lines. This enables a calling process to resume a search. When grep stops after NUM matching lines, it outputs any trailing context lines. When the -c or --count option is also used, grep does not output a count greater than NUM. When the -v or --invert-match option is also used, grep stops after outputting NUM non-matching lines.
--mmap If possible, use the mmap system call to read input, instead of the default read 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.
-o--only-matching Show only the part of a matching line that matches PATTERN.
--label=LABEL Displays input actually coming from standard input as input coming from file LABEL. This is especially useful for tools like zgrep, e.g., gzip -cd foo.gz |grep -H --label=foo something
--line-buffered Use line buffering. This can incur a performance penalty.
-q--quiet--silent Be quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.
-R-r--recursive Read all files under each directory, recursively; this is equivalent to the -d recurse option. Modified by:

--include=PATTERN

Recurse in directories only searching file matching PATTERN.

--exclude=PATTERN

Recurse in directories skip file matching PATTERN.
-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 Windowsgrep 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 NULL 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 -print0perl -0sort -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, which are used in egrep; 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 bracket expression is a list of characters enclosed by [ and ]. It 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.

Within a bracket expression, a 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, [a-d] is equivalent to [abcd]. Many locales sort characters in dictionary order, and in these locales [a-d] is typically not equivalent to [abcd]; it might be equivalent to [aBbCcDd], for example. To obtain the traditional interpretation of bracket expressions, you can use the C locale by setting the LC_ALL environment variable to the value C.

Finally, certain named classes of characters are predefined within bracket expressions, as follows. 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 C 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 nth 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's behavior is affected by the following environment variables:

A locale LC_foo is specified by examining the three environment variables LC_ALLLC_fooLANG, in that order. The first of these variables that is set specifies the locale. For example, if LC_ALL is not set, but LC_MESSAGES is set to pt_BR, then Brazilian Portuguese is used for the LC_MESSAGES locale. The C 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).

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.

GREP_COLOR

Specifies the marker for highlighting.

LC_ALL, LC_COLLATE, LANG

These variables specify the LC_COLLATE locale, which determines the collating sequence used to interpret range expressions like [a-z].

LC_ALL, LC_CTYPE, LANG

These variables specify the LC_CTYPE locale, which determines the type of characters, e.g., which characters are whitespace.

LC_ALL, LC_MESSAGES, LANG

These variables specify the LC_MESSAGES locale, which determines the language that grep uses for messages. The default C locale uses American English messages.

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 ith character of this environment variable's value is 1, do not consider the ith 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.

查看英文版

查看中文版

egrep 例子

egrep "support|help|windows" myfile.txt

在文件myfile.txt中搜索支持帮助和窗口的模式。

egrep '^[a-zA-Z]+$' myfile.txt

匹配myfile.txt中以字母开头的行,该字母也以该行结尾。

egrep -c '^begin|end$' myfile.txt

计算myfile.txt中以单词“ begin ”开头或以单词“ end ”结尾的行数。

egrep "support|help|windows" myfile.txt

Search for patterns of support help and windows in the file myfile.txt.

egrep '^[a-zA-Z]+$' myfile.txt

Match any lines in myfile.txt which begin a line with an alphabetic word which also ends the line.

egrep -c '^begin|end$' myfile.txt

Count the number of lines in myfile.txt which begin with the word 'begin' or end with the word 'end'.

查看英文版

查看中文版

发表评论
评论列表
共0条
点击加载更多