grep基础正则表达式字符说明:

(1)^word:匹配以word开头的内容;

(2)word$:匹配以word结尾的内容;

(3)^$:表示空行



范例1:匹配以m 开头

[root@oldboy-6test]# grep "^m" oldboy.log

my blog is http://oldboy.blog.51cto.com

my qq num is 49000448

my god,i am not oldboy,but OLDBOY!




范例2:匹配以m 结尾

[root@oldboy-6test]# grep "m$" oldboy.log

my blog is http://oldboy.blog.51cto.com

[root@oldboy-6test]# 





范例3:匹配  空行

[root@oldboy-6test]# grep -n "^$" oldboy.log

3:

8:

[root@oldboy-6test]# cat -n oldboy.log

     1  I am oldboy teacher!

     2  I teach linux.

     3

     4  I like badminton ball,billiard ball and chinese chess!

     5  my blog is http://oldboy.blog.51cto.com

     6  our site is http://www.etiantian.org

     7  my qq num is 49000448

     8

     9  not 4900000448

    10  my god,i am not oldboy,but OLDBOY!

[root@oldboy-6test]# 





范例4:排除 空行

[root@oldboy-6test]# grep -v "^$" oldboy.log

I am oldboy teacher!

I teach linux.

I like badminton ball,billiard ball and chinese chess!

my blog is http://oldboy.blog.51cto.com

our site is http://www.etiantian.org

my qq num is 49000448

not 4900000448

my god,i am not oldboy,but OLDBOY!


[root@oldboy-6test]# grep -vn "^$" oldboy.log 

1:I am oldboy teacher!

2:I teach linux.

4:I like badminton ball,billiard ball and chinese chess!

5:my blog is http://oldboy.blog.51cto.com

6:our site is http://www.etiantian.org

7:my qq num is 49000448

9:not 4900000448

10:my god,i am not oldboy,but OLDBOY!

[root@oldboy-6test]#