Linux系列shell编程之文档操作(3)

网友投稿 592 2022-05-29

一、正则表达式和通配符

// 本章知识点讲解文件如下:文件格式不要动,就是这样。 [root@image1 ~]# cat test_rule.sh Mr. Zhang San said; he was the hones man in LampBrother. 123despise him. But since Mr. Li Si came, he never saaaid those words. 5555nice! because, actuaaaally, Mr. Li Si is the most honest man Later, Mr. Zhang San soid his hot body.

1)linux中支持的通配符

* 代表任意字符重复任意多次; ? 代表任意字符重复一次; [] 代表匹配[]中所写的任意一个字符;

* 前一个字符匹配0次,或任意多次。 . 匹配除了换行符外,任意一个字符。 ^ 匹配行首。【^hello匹配以hello开头的行】 $ 匹配行尾。【hello$匹配以hello结尾的行】 [] 匹配中括号中指定的任意一个字符,只匹配一个字符。 [^] 取反。匹配除中括号的字符以外的任意一个字符。 \ 转义符。取消特殊符号的含义,变为普通字符。 \{n\} 表示其前面的字符恰好出现n次。[0-9]\{4\}匹配4位数字 \{n,\} 表示其前面的字符出现不小于n次。[0-9]\{2,\}匹配2位及以上数字 \{n,m\} 表示其前面的字符至少出现n次,最多出现m次。

// 这样写是匹配所有内容,包括空白行 [root@image1 ~]# grep "a*" test_rule.sh // 匹配至少有一个a的行 [root@image1 ~]# grep "aa*" test_rule.sh // 匹配最少包含两个连续a的行 [root@image1 ~]# grep "aaa*" test_rule.sh // 匹配最少包含四个连续a的行 [root@image1 ~]# grep "aaaa*" test_rule.sh

// 匹配在s、d这两个字母之间一定有两个字符的单词 [root@image1 ~]# grep "s..d" test_rule.sh Mr. Zhang San said; Later, Mr. Zhang San soid his hot body. // 匹配在s、d字母之间有在意字符 [root@image1 ~]# grep "s.*d" test_rule.sh Mr. Zhang San said; he never saaaid those words. Later, Mr. Zhang San soid his hot body. // 匹配所有内容 [root@image1 ~]# grep ".*" test_rule.sh

// 匹配以大写“M”开头的行 [root@image1 ~]# grep "^M" test_rule.sh Mr. Zhang San said; Mr. Li Si is the most honest man // 匹配以小写“n”结尾的行 [root@image1 ~]# grep "n$" test_rule.sh Mr. Li Si is the most honest man // 匹配空白行。-n表示显示匹配的是哪一行。 [root@image1 ~]# grep -n "^$" test_rule.sh 7: [root@image1 ~]#

// 匹配s和id中,有一个a或者有一个o的行 [root@image1 ~]# grep "s[ao]id" test_rule.sh Mr. Zhang San said; Later, Mr. Zhang San soid his hot body. // 匹配带有一个数字的行 [root@image1 ~]# grep "[0-9]" test_rule.sh 123despise him. 5555nice! // 匹配不带有一个数字的行 [root@image1 ~]# grep "[^0-9]" test_rule.sh

// 匹配不用小写字母开头的行 [root@image1 ~]# grep "^[^a-z]" test_rule.sh Mr. Zhang San said; 123despise him. But since Mr. Li Si came, 5555nice! Mr. Li Si is the most honest man Later, Mr. Zhang San soid his hot body. // 匹配不用字母开头的行 [root@image1 ~]# grep "^[^a-zA-Z]" test_rule.sh 123despise him. 5555nice!

// 匹配使用“.”结尾的行 [root@image1 ~]# grep "\.$" test_rule.sh he was the hones man in LampBrother. 123despise him. he never saaaid those words. Later, Mr. Zhang San soid his hot body.

// 匹配a字母连续出现三次的字符串 [root@image1 ~]# grep "a\{3\}" test_rule.sh he never saaaid those words. because, actuaaaally, // 匹配包含连续的四个数字的字符串 [root@image1 ~]# grep "[0-9]\{4\}" test_rule.sh 5555nice!

// 匹配至少以三个连续数字开头的字符串。 [root@image1 ~]# grep "^[0-9]\{3,\}" test_rule.sh 123despise him. 5555nice!

// 匹配在字母s和字母i之间有最少一个a,最多三个a的行 [root@image1 ~]# grep "sa\{1,3\}i" test_rule.sh Mr. Zhang San said; he never saaaid those words. // 匹配在字母s和字母i之间有最少一个a,最多两个a的行 [root@image1 ~]# grep "sa\{1,2\}i" test_rule.sh Mr. Zhang San said;

Linux系列:shell编程之文档操作(3)

Linux Shell

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Google鼓励的13条代码审查标准
下一篇:Linux系列:shell编程之文档操作(2)
相关文章