正则表达式,也叫规则表达式
通过规则表达式找到我们想要的东西
^ 锚定行首
[root@tz shell]# cat regex
hello world
hi hello
hello ,zsy
[root@tz shell]# grep "^hello" regex
hello world
hello ,zsy
$ 锚定行尾
[root@tz shell]# grep "hello$" regex
hi hello
^xxx$ 结合使用,既位于行首也位于行尾,即整行只有xxx
参数-n显示行号,参数color显示颜色
[root@tz shell]# cat regex
hello world
hi hello
hello ,zsy
hello
[root@tz shell]# grep -n --color "^hello$" regex
4:hello
^$ 匹配空行
[root@tz shell]# grep -n "^$" regex
3:
< 锚定词首
> 锚定词尾
[root@tz shell]# cat REG abchello world #可以看到hello位于abchello词尾 abc helloabc abc abc abchelloabc abc [root@tz shell]# grep --color "<hello" REG abc helloabc abc [root@tz shell]# grep --color "hello>" REG abchello world
<与> 结合
[root@tz shell]# cat REG
abchello world
abc helloabc abc
abc abchelloabc abc
hello
[root@tz shell]# grep --color "<hello>" REG
hello
<与> 结合使用 ,即既是行首也是行尾,即单独的字符串,可以用代替
锚定词首
[root@tz shell]# grep --color "hello" REG abc helloabc abc hello [root@tz shell]# grep --color "hello" REG abchello world hello [root@tz shell]# grep --color "hello" REG hello
B 锚定非词首
只要词首不是hello都会被匹配
[root@tz shell]# grep --color "Bhello" REG
abchello world
abc abchelloabc abc
abchello helloabc hello ahelloa
学习来自朱双印朱老师的博客