一、使用正则表达式匹配行首行尾,需要注意空格与

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工具时,作者应该是考虑到,尽量匹配到所有有用的信息,不丢失信息,缩小范围可以放在下一步。


三、使用grep参数之间的影响


[root@c1 ~]# grep -in '^$' regular_express.txt

2:4:6:8:10:12:14:16:18:20:22:24:26:28:30:32:34:36:38:40:42:43:

[root@c1 ~]# grep -n '^$' regular_express.txt

22:


在使用grep搜索空格时,加大小写忽略会影响到输出结果,原因未知。


四、使用管道命令时,前面命令参数会影响到后面的结果:

   

[root@c1 ~]# cat -n regular_express.txt | grep -v '^$' | grep -v '^#'

    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.

   12  the symbol '*' is represented as start.

   13  Oh!     My god!

   14  The gd software is a library for drafting programs.^M

   15  You are the best is mean you are the no. 1.

   16  The world <Happy> is the same with "glad".

   17  I like dog.

   18  google is the best tools for search keyword.

   19  goooooogle yes!

   20  go! go! Let's go.

   21  # I am VBird

   22

   错误结果

[root@c1 ~]# cat regular_express.txt | grep -v '^$' | grep -v '^#'

  ource" is a good mechanism to develop programs.

  apple is my favorite food.      

Football game is not use feet only.    

this dress doesn't fit me.

However, this dress is about $ 3183 dollars.^M

GNU is free air not free beer.^M

Her hair is very beauty.^M

I can't finish the test.^M

Oh! The soup taste good.^M

motorcycle is cheap than car.

This window is clear.

the symbol '*' is represented as start.

Oh!     My god!

The gd software is a library for drafting programs.^M

You are the best is mean you are the no. 1.

The world <Happy> is the same with "glad".

I like dog.

google is the best tools for search keyword.

goooooogle yes!

go! go! Let's go.

正确结果