一、使用正则表达式匹配行首行尾,需要注意空格与
1.vi文本在每行的末尾都会有影藏字符$,表示本行的结束,那么在一行的结束加上空格或者换行符,那 么隐藏字符$也会跟在空格或者换行符之后,那么如果不注意这个在搜索时就会出错。
如:
[root@c1 ~]# cat -An regular_express.txt
1 ource" is a good mechanism to develop programs.$
2 apple is my favorite food. $
3 Football game is not use feet only. $
4 this dress doesn‘t fit me.$
5 However, this dress is about $ 3183 dollars.^M$
6 GNU is free air not free beer.^M$
7 Her hair is very beauty.^M$
8 I can‘t finish the test.^M$
9 Oh! The soup taste good.^M$
10 motorcycle is cheap than car.$
11 This window is clear.$
这是如果想要使用grep 搜索以.结尾的字符,不注意空格或者换行符,其结果:
[root@c1 ~]# cat -n regular_express.txt | grep ‘\.$‘
1 ource" is a good mechanism to develop programs.
4 this dress doesn‘t fit me.
10 motorcycle is cheap than car.
11 This window is clear.
那么符合标准的2、3行则会搜索不出来。
2. 以多个空格开头,后面接着字符,在使用^搜索时也会有这样的问题:
[root@c1 ~]# grep -n ‘^[a-z]‘ regular_express.txt
4:this dress doesn‘t fit me.
10:motorcycle is cheap than car.
没有搜索出1、2行
补充:
使用 cat -n regular_express.txt | grep ‘^[a-z]‘ 没有输出正确的结果,很奇怪
管道符使用需谨慎!
二、正则表达式中,以搜索匹配的字符作为目的,感觉还少则【逻辑非】,用来缩小匹配范围。
这则表达式 grep 用法小实验,布布扣,bubuko.com
原文:http://2847513.blog.51cto.com/2837513/1377282