每天一个linux命令(39):grep 命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。
grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须用引号包起来,模板后的所有字符串被看作文件名。搜索的结果被送到标准输出,不影响原文件内容。
grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。我们利用这些返回值就可进行一些自动化的文本处理工作。
grep, egrep, fgrep, rgrep - print lines matching a pattern.
1.命令格式:
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
2.命令功能:
用于过滤/搜索的特定字符。可使用正则表达式能多种命令配合使用,使用上十分灵活。
grep searches the named input FILEs (or standard input if no files are named, or if a single (连字号)hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
In addition, three variant programs egrep, fgrep and rgrep are available. egrep is the same as grep -E. fgrep is the same as grep -F. rgrep is the same as grep -r. Direct invocation as either egrep or fgrep is deprecated(弃用), but is provided to allow historical applications that rely on them to run unmodified.
3.命令参数:
Generic Program Information
--help Print a usage message briefly summarizing these command-line options and the bug-reporting address, then exit.
-V, --version Print the version number of grep to the standard output stream. This version number should be included in all bug reports (see below).
Matcher Selection
-E, --extended-regexp Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)
-F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.) 需要与-f配合使用? ANSWER: -F意为把PATTERN当做固定字符串(普通字符串),不作为正则表达式。与-f没有关系。
-G, --basic-regexp Interpret PATTERN as a basic regular expression (BRE, see below). This is the default. (默认为此) (General普通?)
-P, --perl-regexp Interpret PATTERN as a Perl regular expression. This is highly experimental(试验性的) and grep -P may warn of unimplemented(未实现的) features.
Matching Control
-e PATTERN, --regexp=PATTERN Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen(-). (-e is specified by POSIX.)
-f FILE, --file=FILE Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by POSIX.)
-i, --ignore-case Ignore case distinctions in both the PATTERN and the input files. (-i is specified by POSIX.) 默认是大小写敏感。
-v, --invert-match Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.) 用于剔除。
-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. (-x is specified by POSIX.)
-y Obsolete(废弃) synonym(同义词) for -i.
General Output Control
-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. (-c is specified by POSIX.)
--color[=WHEN], --colour[=WHEN] Surround(围绕) the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines) with escape sequences(转义序列???) to display them in color on the terminal. The colors are defined by the environment variable GREP_COLORS. The deprecated(弃用的) environment variable GREP_COLOR is still supported, but its setting does not have priority. WHEN is never, always, or auto.
-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. 列出文件内容不符合指定的样式的文件名称。(就专门找那些不包含匹配的文件,并打印它的名字? ANSWER:对。)
-l(小写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. 列出文件内容符合指定的样式的文件名称。(是只找一个包含匹配的文件,还是找这些所有文件的时候,一旦发现有匹配就转为检查下一个文件;并打印它的名字? ANSWER:找所有。这样就不显示行,只显示文件名了) (-l is specified by POSIX.)
-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.
-o, --only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
-q, --quiet, --silent 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. (-q is specified by POSIX.) 专门适合编写脚本?
-s, --no-messages Suppress error messages about nonexistent or unreadable files. Portability(可移植性) note: unlike GNU grep, 7th Edition Unix grep did not conform to(不符) POSIX, because it(7th Edition) lacked -q and its -s(7th Edition) option behaved like GNU grep's -q option. USG-style grep also lacked -q but its -s option behaved like GNU grep. Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead. (-s is specified by POSIX.)
Output Line Prefix Control
-b, --byte-offset Print the 0-based(从零开始) byte offset within the input file before each line of output. If -o (--only-matching) is specified, print the offset of the matching part itself.? ANSWER:看下面的例子,完全搞不懂。pass。
kenm6@ken6-desktop:~/liujin/pratice$ cat cfiles.txt
1B
12345B
123456789B
B
kenm6@ken6-desktop:~/liujin/pratice$ grep -b B cfiles.txt
0:1B
3:12345B
10:123456789B
21:B
kenm6@ken6-desktop:~/liujin/pratice$
-H, --with-filename Print the file name for each match. This is the default when there is more than one file to search.
-h, --no-filename Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.
--label=LABEL Display 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 --label=foo something.???
-n, --line-number Prefix each line of output with the 1-based line number within its input file. (-n is specified by POSIX.)
-T, --initial-tab Make sure that the first character of actual line content lies on a tab stop(制表位), so that the alignment of tabs looks normal. This is useful with options that prefix their output to the actual content: -H,-n, and -b. In order to improve the probability that lines from a single file will all start at the same column, this also causes the line number and byte offset (if present) to be printed in a minimum size field width. 就是在行号、byte-offset列对齐输出。
-u, --unix-byte-offsets Report Unix-style byte offsets. This switch causes grep to report byte offsets as if the file were a 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.
-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.
Context Line Control
-A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing a group separator (--) between contiguous(相邻的) groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.
-B NUM, --before-context=NUM Print NUM lines of leading context before matching lines. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.
-C NUM, -NUM, --context=NUM Print NUM lines of output context. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given. 除了显示符合样式的那一行之外,并显示该行之前、后的内容。
File and Directory Selection
-a, --text
Process a binary file as if it were text; this is equivalent to the --binary-files=text option.
--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. 真JB啰嗦,用-a,-I就代替了吧。
-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 recurse, grep reads all files under each directory, recursively;
this is equivalent to the -r option.
--exclude=GLOB
Skip files whose base name matches GLOB (using wildcard(通配符) matching). A file-name glob can use *, ?, and [...] as wildcards, and \ to quote a wildcard or backslash character literally.
--exclude-from=FILE
Skip files whose base name matches any of the file-name globs read from FILE (using wildcard matching as described under --exclude).
--exclude-dir=DIR
Exclude directories matching the pattern DIR from recursive searches. DIR可以是目录名(base name),也可以是路径名。如果是从当前路径开刷,前面不能少了./
-I Process a binary file as if it did not contain matching data; this is equivalent to the --binary-files=without-match option. 忽略二进制文件。
--include=GLOB
Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).
-R, -r, --recursive
Read all files under each directory, recursively; this is equivalent to the -d recurse option.
Other Options
--line-buffered
Use line buffering on output. This can cause a performance penalty(性能代价).
--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.
-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.
-z, --null-data
Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null option,
this option can be used with commands like sort -z to process arbitrary file names.
4.规则表达式:
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 meta-character(元字符) with special meaning may be quoted by preceding it with a backslash.
The period . matches any single character.
Character Classes and Bracket Expressions
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(前者,指[[:alnum:]]) 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 expression.???) Most meta-characters lose their special meaning inside bracket expressions. 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.
Anchoring(锚定)
The caret ^ and the dollar sign $ are meta-characters that respectively(分别) match the empty string at the beginning and end of a line.
The Backslash Character and Special Expressions
The symbols \< and \> (vim里的正则表达式用这个锚定单词) 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. 如: '\bgrep\b'只匹配grep,意即单词grep前后都是空白,\b的b即blank。'\bgrep\b'='\<grep\>'? The symbol \w is a synonym for [[:alnum:]] and \W is a synonym for [^[:alnum:]].
Repetition
A regular expression may be followed by one of several repetition operators:
? The preceding item is optional and matched at most(至多) once. (0或者1次?即:有无)
* The preceding item will be matched zero or more times. (至少0次)
+ The preceding item will be matched one or more times. (至少1次)
{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 most m times. (最多m次)
{n,m} The preceding item is matched at least n times, but not more than m times.
Concatenation
Two regular expressions may be concatenated; the resulting regular expression matches any string formed by concatenating two substrings that respectively(分别) match the concatenated expressions.
Alternation(交互)
Two regular expressions may be joined by the infix operator(插入运算符) |; the resulting regular expression matches any string matching either alternate expression.
Precedence(优先级)
Repetition takes precedence over concatenation, which in turn(反过来) takes precedence over alternation(Repetition>concatenation>alternation). A whole expression may be enclosed in(关在…内) parentheses(圆括号) to override these precedence rules and form(组成) a subexpression.
Back References and Subexpressions
The back-reference(反向引用) \n, where n is a single digit, matches the substring previously(预先) matched by the nth parenthesized(将……加上括弧) subexpression of the regular expression.
Basic vs Extended Regular Expressions
In basic regular expressions(grep默认) the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(,and \).
Traditional egrep did not support the { meta-character(元字符意为它本身吧), and some egrep implementations support \{ instead, so portable scripts should avoid { in grep -E patterns and should use [{] to match a literal {.
GNU grep -E 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 command grep -E '{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.
POSIX字符:
为了在不同国家的字符编码中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字符类,如[:alnum:]是[A-Za-z0-9]的另一个写法。要把它们放到[]号内才能成为正则表达式,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支持POSIX的字符类。
[:alnum:] #文字数字字符
[:alpha:] #文字字符
[:digit:] #数字字符
[:graph:] #非空字符(非空格、控制字符)
[:lower:] #小写字符
[:cntrl:] #控制字符
[:print:] #非空字符(包括空格)
[:punct:] #标点符号
[:space:] #所有空白字符(新行,空格,制表符)
[:upper:] #大写字符
[:xdigit:] #十六进制数字(0-9,a-f,A-F)
5.使用实例:
实例0:根据文件内容递归查找目录
# grep ‘energywise’ * #在当前目录搜索带'energywise'行的文件 # grep -r ‘energywise’ * #在当前目录及其子目录下搜索'energywise'行的文件
# grep -l -r ‘energywise’ * #在当前目录及其子目录下搜索'energywise'行的文件,但是不显示匹配的行,只显示匹配的文件
kenm1@ken1-server:~/liujin/1128/custom/s8_v41_jb3_w/jeed$ grep -irn "CUSTOM_MODEM" ./*/*/P*.mk
./e9650/config/ProjectConfig.mk:1325:CUSTOM_MODEM=mbk72_wet_jb3_hspa_s8_v4.3_Sky77590
./e9660/config/ProjectConfig.mk:1324:CUSTOM_MODEM=mbk72_wet_jb3_hspa_B1B2B5B8
kenm1@ken1-server:~/liujin/1128/custom/s8_v41_jb3_w/jeed$ grep -irn "CUSTOM_MODEM" ./*/*/*/P*.mk
./jeed_e9880/jeed_e9880/config/ProjectConfig.mk:1321:CUSTOM_MODEM=mbk72_wet_jb3_hspa_s8_v4.3_Sky77584
./s8_6572_smt_207/s8_6572_smt_207/config/ProjectConfig.mk:1321:CUSTOM_MODEM=mbk72_wet_jb3_hspa_s8_v4.3_Sky77590
./s8_6572_smt/s8_6572_smt/config/ProjectConfig.mk:1321:CUSTOM_MODEM=mbk72_wet_jb3_hspa_s8_v4.3_Sky77590
这个办法还是有点笨,别忘了find命令,它可是专门搜索文件的,将它的结果给grep,那岂不是绝配!!!
cd custom/s8_v41_jb3_w
find -name "P*.mk"|grep -irn CUSTOM_MODEM
还有更简洁的:哈哈哈! grep -irn CUSTOM_MODEM `find -name P*.mk`
注意:注意包裹find命令的不是单引号,而是ESC键下面一个键的符号
实例1:查找指定进程
命令:
ps -ef|grep svn
输出:
[root@localhost ~]# ps -ef|grep svn
root 4943 1 0 Dec05 ? 00:00:00 svnserve -d -r /opt/svndata/grape/
root 16867 16838 0 19:53 pts/0 00:00:00 grep svn
[root@localhost ~]#
说明:
第一条记录是查找出的进程;第二条结果是grep进程本身,并非真正要找的进程。
实例2:查找指定进程个数
命令:
ps -ef|grep svn -c
ps -ef|grep -c svn
输出:
[root@localhost ~]# ps -ef|grep svn -c
2
[root@localhost ~]# ps -ef|grep -c svn
2
[root@localhost ~]#
说明:
实例3:从文件中读取关键词进行搜索
命令:
cat test.txt | grep -f test2.txt
输出:
[root@localhost test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt
linux
Redhat
[root@localhost test]# cat test.txt | grep -f test2.txt
hnlinux
ubuntu linux
Redhat
linuxmint
[root@localhost test]#
说明:
输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行
实例3:从文件中读取关键词进行搜索且显示行号
命令:
cat test.txt | grep -nf test2.txt
输出:
[root@localhost test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt
linux
Redhat
[root@localhost test]# cat test.txt | grep -nf test2.txt
1:hnlinux
4:ubuntu linux
6:Redhat
7:linuxmint
[root@localhost test]#
说明:
输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行,并显示每一行的行号
实例5:从文件中查找关键词
命令:
grep 'linux' test.txt
输出:
[root@localhost test]# grep 'linux' test.txt
hnlinux
ubuntu linux
linuxmint
[root@localhost test]# grep -n 'linux' test.txt
1:hnlinux
4:ubuntu linux
7:linuxmint
[root@localhost test]#
说明:
实例6:从多个文件中查找关键词
命令:
grep 'linux' test.txt test2.txt
输出:
[root@localhost test]# grep -n 'linux' test.txt test2.txt
test.txt:1:hnlinux
test.txt:4:ubuntu linux
test.txt:7:linuxmint
test2.txt:1:linux
[root@localhost test]# grep 'linux' test.txt test2.txt
test.txt:hnlinux
test.txt:ubuntu linux
test.txt:linuxmint
test2.txt:linux
[root@localhost test]#
说明:
多文件时,输出查询到的信息内容行时,会把文件的命名在行最前面输出并且加上":"作为标示符
实例7:grep不显示本身进程
命令:
ps aux|grep \[s]sh
ps aux | grep ssh | grep -v "grep"
输出:
[root@localhost test]# ps aux|grep ssh
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
root 16901 0.0 0.0 61180 764 pts/0 S+ 20:31 0:00 grep ssh
[root@localhost test]# ps aux|grep \[s]sh]
[root@localhost test]# ps aux|grep \[s]sh
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
[root@localhost test]# ps aux | grep ssh | grep -v "grep"
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
说明:???
实例8:找出已u开头的行内容
命令:
cat test.txt |grep ^u
输出:
[root@localhost test]# cat test.txt |grep ^u
ubuntu
ubuntu linux
[root@localhost test]#
说明:
实例9:输出非u开头的行内容
命令:
cat test.txt |grep ^[^u]
输出:
[root@localhost test]# cat test.txt |grep ^[^u]
hnlinux
peida.cnblogs.com
redhat
Redhat
linuxmint
[root@localhost test]#
说明:
实例10:输出以hat结尾的行内容
命令:
cat test.txt |grep hat$
输出:
[root@localhost test]# cat test.txt |grep hat$
redhat
Redhat
[root@localhost test]#
说明:
实例11:
命令:
输出:
[root@localhost test]# ifconfig eth0|grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test]# ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"
inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test]#
说明:-E --extended-regexp #将样式为延伸的普通表示法来使用。
实例12:显示包含ed或者at字符的内容行
命令:
cat test.txt |grep -E "ed|at"
输出:
[root@localhost test]# cat test.txt |grep -E "peida|com"
peida.cnblogs.com
[root@localhost test]# cat test.txt |grep -E "ed|at"
redhat
Redhat
[root@localhost test]#
说明:
实例13:显示当前目录下面以.txt 结尾的文件中的所有包含每个字符串至少有7个连续小写字符的字符串的行
命令:
grep '[a-z]\{7\}' *.txt
输出:
[root@localhost test]# grep '[a-z]\{7\}' *.txt
test.txt:hnlinux
test.txt:peida.cnblogs.com
test.txt:linuxmint
[root@localhost test]#
说明:

浙公网安备 33010602011771号