19027008.doc Confidentiality level 密级
实例讲解
一.模式匹配
1.ls显示所有以hosts.开头的文件
ls –l hosts.*
2.ls显示包含x,y,z字符的所有文件 ls –d *[x-z]*
1.grep匹配/etc/services文件中以ftp字符串开头的哪些文本行
grep ?^ftp‘ /etc/services
9.grep匹配以ftp开头,后跟0个或多个-agent的文本行 grep ?^ftp(-agent)?‘ /etc/services
或 grep 注:
10.grep匹配以ftp开头,后跟1个或多个-agent的文本行 grep ?^ftp(-agent)+‘ /etc/services
Page 21 , Total 83 第21页,共83页
二.正则表达式
2.grep匹配以system文本结尾的行。 grep ?system$‘ file 3.grep匹配仅包含一个#字符的行。 grep ?^#$‘ file 4.grep匹配以或者[abc]开头的行 grep ?[[<]abc[]>]‘ file 5.grep匹配以Ftp或者ftp开头的行 grep ?^[Ff]tp‘ file 6.grep匹配F或者f以外的字符 grep ?[^Ff]‘ file 7.grep匹配除大写字符以外的字符 grep ?[^A-Z]‘ file 8.grep匹配以ftp或telnet开头的文本行 grep -E ?^ftp|^telnet‘ file
?^ftp(-agent)*‘ /etc/services
a)在sco unix下,上面的单括号前要加转义符\\ b)在sun os 5.8下,不论加不加单括号均不支持。
All rights reserved 版权所有,侵权必究
19027008.doc Confidentiality level 密级
说明同上。
11.grep匹配带有数字6,后跟至少3个0的文本行(使用-E启用边界特性) grep -E ?60\\{3,\\}‘ /etc/services 12.grep匹配含有(abc)的文本 grep ?\\(abc\\)‘ file 13.常用正则表达式举例 正则表达式 ^[the] [Ss]igna[lL] 匹配功能 以the开头行 匹配单词signal,signaL,Signal,SignaL [Ss]igna[lL]\\. [mayMAY] ^USER$ [tty]$ \\. ^d..x..x..x ^[^l] [.*0] [000*] [iI] [iI][nN] [^$] [^.*$] ^……$ [a-zA-Z] [a-z][a-z]* [^0-9\\$] [^0-9A-Za-z] [123] [Dd]evice De..ce ^.$ ^\\.[0-9][0-9] ―‘Device‘‖ De[Vv]ice\\. [0-9]\\{2\\}-[0-9]\\{2\\}-[0-9]\\{4\\} 同上,但加一个句点 只包含USER的行 以tty结尾的行 带句点的行 对用户、用户组及其它用户组成员有可执行权限的目录 排除关联目录的目录列表 0之前或之后加任意字符 000或更多个 大写或小写I 大写或小写I或n 空行 匹配行中任意字符 包括6各字符的行 任意单字符 至少两个小写字母 非数字或美元表示 非数字或字母 1到3中的一个数字 单词Device或device 前两个字母为De,后跟两个任意字符,最后为ce 仅有一个字符的行 以一个句点和两个数字开始的行 单词Device 单词Device.或DeVice. 日期格式dd-mm-yyyy Page 22 , Total 83 第22页,共83页
All rights reserved 版权所有,侵权必究
19027008.doc Confidentiality level 密级
[1-9][0-9]\\{1,2\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\} [^.*$]
16.特殊字符:$ . ? ― * [ ] ^ | \\ + ?
15.grep消除大小写:加入-i选项 grep -I ―sept‖ file
14.grep精确匹配:在抽取字符串后加\\>。 grep ―48\\>‖ file
IP地址格式nnn.nnn.nnn.nnn 匹配任意行 如果要查询这些字符,需要在前面加转义字符\\。
17.grep判断变量含有[HOST]字符串 if [ \
18.grep判断变量含有[xxx]字符串 if [ \
19.grep匹配后缀为c,h,j,s,cpp,hpp的文件
EXT_ALL='chjs'
EXT_PP='ch' EXT_NO_PP='js'
ls $1 | grep \
\
20.使用grep在文件中查找变量
grep `echo $user` /etc/passwd | cut –f5 –d?:‘
21.正则表达式语法
来自msdn,仅供参考。
Here are some examples of regular expressions:
/^\\s*$/ /\\d{2}-\\d{5}/ Expression Matches Match a blank line. Validate an ID number consisting of 2 digits, a hyphen, and an additional 5 digits. /<\\s*(\\S+)(\\s[^>]*)?>[\\s\\S]*<\\s*\\/\\1\\s*>/ Match an HTML tag. All rights reserved 版权所有,侵权必究
Page 23 , Total 83 第23页,共83页
19027008.doc Confidentiality level 密级
The following table contains the complete list of metacharacters and their behavior in the context of regular expressions: Character \\ Description Marks the next character as a special character, a literal, a backreference, or an octal escape. For example, 'n' matches the character \sequence '\\\\' matches \^ Matches the position at the beginning of the input string. If the RegExp object's Multiline property is set, ^ also matches the position following '\\n' or '\\r'. $ Matches the position at the end of the input string. If the RegExp object's Multiline property is set, $ also matches the position preceding '\\n' or '\\r'. * Matches the preceding character or subexpression zero or more times. For example, zo* matches \and \* is equivalent to {0,}. + Matches the preceding character or subexpression one or more times. For example, 'zo+' matches \and \but not \+ is equivalent to {1,}. ? Matches the preceding character or subexpression zero or one time. For example, \\{n} n is a nonnegative integer. Matches exactly n times. For example, 'o{2}' does not match the 'o' in \but matches the two o's in \{n,} n is a nonnegative integer. Matches at least n times. For example, 'o{2,}' does not match the \in \and matches all the o's in \'o{0,}' is equivalent to 'o*'. {n,m} M and n are nonnegative integers, where n <= m. Matches at least n and at most m times. For example, \the first three o's in \'o{0,1}' is equivalent to 'o?'. Note that you cannot put a space between the comma and the numbers. ? When this character immediately follows any of the other quantifiers (*, +, ?, {n}, {n,}, {n,m}), the matching pattern is non-greedy. A non-greedy pattern matches as little of the searched string as possible, whereas the default greedy pattern matches as much of the searched string as possible. For example, in the string \'o+?' matches a single \ All rights reserved 版权所有,侵权必究
Page 24 , Total 83 第24页,共83页
19027008.doc Confidentiality level 密级
while 'o+' matches all 'o's. . Matches any single character except \character including the '\\n', use a pattern such as '[\\s\\S]'. (pattern) A subexpression that matches pattern and captures the match. The captured match can be retrieved from the resulting Matches collection using the $0…$9 properties. To match parentheses characters ( ), use '\\(' or '\\)'. (?:pattern) A subexpression that matches pattern but does not capture the match, that is, it is a non-capturing match that is not stored for possible later use. This is useful for combining parts of a pattern with the \'industr(?:y|ies) is a more economical expression than 'industry|industries'. A subexpression that performs a positive lookahead search, (?=pattern) which matches the string at any point where a string matching pattern begins. This is a non-capturing match, that is, the match is not captured for possible later use. For example 'Windows (?=95|98|NT|2000)' matches \2000\consume characters, that is, after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead. A subexpression that performs a negative lookahead search, (?!pattern) which matches the search string at any point where a string not matching pattern begins. This is a non-capturing match, that is, the match is not captured for possible later use. For example 'Windows (?!95|98|NT|2000)' matches \in \3.1\but does not match \in \2000\Lookaheads do not consume characters, that is, after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead. x|y [xyz] [^xyz] [a-z] Matches either x or y. For example, 'z|food' matches \\A character set. Matches any one of the enclosed characters. For example, '[abc]' matches the 'a' in \A negative character set. Matches any character not enclosed. For example, '[^abc]' matches the 'p' in \A range of characters. Matches any character in the specified range. For example, '[a-z]' matches any lowercase alphabetic character in the range 'a' through 'z'. All rights reserved 版权所有,侵权必究
Page 25 , Total 83 第25页,共83页