本文共 4813 字,大约阅读时间需要 16 分钟。
最近在看正则表达式部分的东西,有些东西记录下.
grep 'ro\{2\}t' 匹配root
3.2 {n,} 匹配前面的字符至少n次以上(含n次) grep 'ro\{0,\}t 匹配以r开头,t结尾, 包含o 0到多次'
3.3 {n,m} 匹配前面的字符n到m次.“^root”匹配的是以字母root开始的行
“abc$”代表的是以abc结尾的行
单项选择题的答案,可能是A、B、C、D选项中的任意一种,用正则表达式表示就是[ABCD]。如果遇到比较大范围的匹配,比如说要匹配任意一个大写字母,就需要使用“-”号做范围限定,写成[A-Z],要匹配所有字母则写成[A-Za-z]。一定要注意,这里“-”的作用不是充当一个字符
“\<hello”用于匹配以“hello”开头的单词;而“hello\>”则用于匹配以“hello”结尾的单词. 还可以使用它们的组合—“\<\>”用于精确匹配一个字符串。所以“\<hello\>”可精确匹配单词hello
“\bhello\b”可精确匹配“hello”单词。
hello\B可以匹配“helloworld”中的“hello”
“ro?t”仅能匹配rot或rt。
“ro+t”就可以匹配rot、root等
[hadoop@bdata01 shell ]$ cat RegExp.txt ----------TEXT BEGIN-----------good morning teacherhello world is a scriptgold sunshine looks beautifulgolden time fliesgod bless mewhat a delicious foodthey teast Goodyou fell gladwrong word goooodwrong word g10dwrong word g12dwrong word g13dwww.helloworld.comwww@helloworld@comUpper case100% means purephp have a gd module-----------TEXT END----------------#1. 搜索含有good的单词的行, 注意: grep默认是区分大小写的,所以这里只会打印有good的行[hadoop@bdata01 shell ]$ grep 'good' RegExp.txt good morning teacher#2. 搜索含有good单词的行,不区分大小写[hadoop@bdata01 shell ]$ grep -i 'good' RegExp.txt good morning teacherthey teast Good#3. 统计不含good单词的行,不区分大小写[hadoop@bdata01 shell ]$ grep -ivc 'good' RegExp.txt 19#4. 搜索以good开头的行[hadoop@bdata01 shell ]$ grep '^good' RegExp.txt good morning teacher#5. 搜索以Good结尾的行[hadoop@bdata01 shell ]$ grep 'Good$' RegExp.txt they teast Good#6. 搜索空行行数[hadoop@bdata01 shell ]$ grep -c '^$' RegExp.txt 2#7. 搜索包含good 和 Good的行[hadoop@bdata01 shell ]$ grep '[Gg]ood' RegExp.txt good morning teacherthey teast Good#8. 搜索一个包含ood的行,但不能是good 和 Good,使用反选功能[hadoop@bdata01 shell ]$ grep '[^Gg]ood' RegExp.txt what a delicious foodwrong word gooood#9. 搜索一个以g开头,中间两个任意字符,结尾是d的行[hadoop@bdata01 shell ]$ grep 'g..d' RegExp.txt good morning teachergold sunshine looks beautifulgolden time fliesyou fell gladwrong word g10dwrong word g12dwrong word g13d#10. 搜索一个以g或者G开头,中间两个任意字符,结尾是d的行[hadoop@bdata01 shell ]$ grep '[Gg]..d' RegExp.txt good morning teachergold sunshine looks beautifulgolden time fliesthey teast Goodyou fell gladwrong word g10dwrong word g12dwrong word g13d#11. 搜索以g或G开头,中间是1或者o,第三个字符是任意字符,最后以d结尾的行[hadoop@bdata01 shell ]$ grep '[Gg][1o].d' RegExp.txt good morning teachergold sunshine looks beautifulgolden time fliesthey teast Goodwrong word g10dwrong word g12dwrong word g13d#12. 搜索含有gold的行, 精确匹配[hadoop@bdata01 shell ]$ grep '\' RegExp.txt gold sunshine looks beautiful[hadoop@bdata01 shell ]$ grep '\bgold\b' RegExp.txt gold sunshine looks beautiful#13. 搜索以g开头,中间包含0到无限个o,以d结尾的行[hadoop@bdata01 shell ]$ grep go*d RegExp.txt good morning teachergod bless mewrong word goooodphp have a gd module#14. 搜索以g开头,中间包含字符,以d结尾的行[hadoop@bdata01 shell ]$ grep 'g.*d' RegExp.txt good morning teachergold sunshine looks beautifulgolden time fliesgod bless meyou fell gladwrong word goooodwrong word g10dwrong word g12dwrong word g13dphp have a gd module#15. 搜索把o错写成数字的行[hadoop@bdata01 shell ]$ grep 'g1[0-9]d' RegExp.txt wrong word g10dwrong word g12dwrong word g13d#16. 搜索包含www.helloworld.com的行[hadoop@bdata01 shell ]$ grep 'www.helloworld.com' RegExp.txt www.helloworld.comwww@helloworld@com这里需要对.进行转义[hadoop@bdata01 shell ]$ grep 'www\.helloworld\.com' RegExp.txt www.helloworld.com#17. 搜索一个将good拼错,写了多个额o的行#搜两个o及以上的行[hadoop@bdata01 shell ]$ grep 'go\{2,\}' RegExp.txt good morning teacherwrong word gooood#搜有四个o的行[hadoop@bdata01 shell ]$ grep 'go\{4\}' RegExp.txt wrong word gooood#18.搜索g和d之间至少有一个o的行[hadoop@bdata01 shell ]$ egrep 'go+d' RegExp.txt good morning teachergod bless mewrong word gooood#19. 搜索g和d之间只有0个或者1个o的行[hadoop@bdata01 shell ]$ egrep 'go?d' RegExp.txt god bless mephp have a gd module#20. 搜索有glad或者 gold的行[hadoop@bdata01 shell ]$ egrep 'glad|gold' RegExp.txt gold sunshine looks beautifulgolden time fliesyou fell glad[hadoop@bdata01 shell ]$ egrep 'g(la|ol)d' RegExp.txt gold sunshine looks beautifulgolden time fliesyou fell glad
参考《Linux系统命令及shell脚本实践指南》
转载地址:http://dkjkb.baihongyu.com/