Linux三剑客之老三grep

说明:

Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。工作中我们常常用它来过滤出我们想要的数据。

格式:

grep [OPTIONS]

基本参数:          

  -i    不区分大小写

  -v   排除内容,即取反

  -n    对匹配到的内容打印相应行号

  -E    使用扩展正则表达式(相当于egrep)

  -r    递归读取目录下的文件(即包括子目录下文件)

  -c    对匹配到的行进行计数

  -o    只显示匹配到的内容

  -A    (after)匹配输出内容行并输出内容行后的指定行

  -B    (before)匹配输出内容行并输出内容行前的指定行

  -C    (context)匹配输出内容行并输出内容行的前后指定行

  --color   过滤的内容显示颜色

建议配置别名:

echo “alias grep='grep --color'” >> /etc/profile //配置别名

       source /etc/profile                                       //重读文件使别名生效

效果:

[root@vm1 test]# grep ".*" /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

常见用法:

①   grep -ivnEoc… “匹配内容” 文件名

[root@vm1 test]# grep -n "root" /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

11:operator:x:11:0:operator:/root:/sbin/nologin

②   grep -r “匹配内容” 文件目录/*

[root@vm1 ~]# grep -r "www" test/*       

test/Caiyun.txt:My blog is http://www.cnblogs.com/Caiyundo/

test/dudu/3.txt:www.cnblog.com/caiyun

③   grep “匹配内容” -A 指定行 文件名

[root@vm1 ~]# grep "root:x" -nA 5 /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

2-bin:x:1:1:bin:/bin:/sbin/nologin

3-daemon:x:2:2:daemon:/sbin:/sbin/nologin

4-adm:x:3:4:adm:/var/adm:/sbin/nologin

5-lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

6-sync:x:5:0:sync:/sbin:/bin/sync

④   命令 | grep “匹配内容”

[root@vm1 ~]# ps -ef |grep "sshd"

root       1261      1  0 Dec12 ?        00:00:00 /usr/sbin/sshd   

扩展:

正则表达式是为处理大量字符串而定义的一套规则和方法,linux正则表达式常用于linux三剑客中。

测试场景模拟(直接复制粘贴即可):

cat  >>text.txt<<EOF

       My name is Caiyun.

       I like badminton, snooker, running

      

       Maybe I'm not a smart person, but I'm working hard.

       My blog is http://www.cnblogs.com/Caiyundo/

       Welcome to my blog! CAIYUN

      

       My qq is 791111890

       My mail is Caiyun111111@gmail.com.

       EOF
View Code

一、 基础正则表达式

 ^ 表示以...字符开头,^word

[root@vm1 test]# grep -n "^My" text.txt

1:My name is Caiyun.

5:My blog is http://www.cnblogs.com/Caiyundo/

8:My qq is 791111890

9:My mail is Caiyun111111@gmail.com.

$    表示以...字符结尾,word$

[root@vm1 test]# grep -n ".$" text.txt 

1:My name is Caiyun.

4:Maybe I'm not a smart person, but I'm working hard.

9:My mail is Caiyun111111@gmail.com.

^$ 表示空字符,即可理解为空行

[root@vm1 test]# grep -n "^$" text.txt  

3:

7:

.     表示匹配任意单个字符

[root@vm1 test]# grep -n "b." text.txt 

2:I like badminton, snooker, running

4:Maybe I'm not a smart person, but I'm working hard.

5:My blog is http://www.cnblogs.com/Caiyundo/

6:Welcome to my blog! CAIYUN

    表示转义符

[root@vm1 test]# grep -n ".$" text.txt

1:My name is Caiyun.

4:Maybe I'm not a smart person, but I'm working hard.

9:My mail is Caiyun111111@gmail.com.

*     表示重复前面0个或1个以上字符

[root@vm1 test]# grep -n "bl*" text.txt 

2:I like badminton, snooker, running

4:Maybe I'm not a smart person, but I'm working hard.

5:My blog is http://www.cnblogs.com/Caiyundo/

6:Welcome to my blog! CAIYUN

.*    表示匹配所有

[root@vm1 test]# grep -n ".*" text.txt   

1:My name is Caiyun.

2:I like badminton, snooker, running

3:

4:Maybe I'm not a smart person, but I'm working hard.

5:My blog is http://www.cnblogs.com/Caiyundo/

6:Welcome to my blog! CAIYUN

7:

8:My qq is 791111890

9:My mail is Caiyun111111@gmail.com.

[]     表示匹配"[]"里面任意单个字符

[root@vm1 test]# grep -n "[791]" text.txt  

8:My qq is 791111890

9:My mail is Caiyun111111@gmail.com.    

[^]   表示取反"[^]"里面的任意单个字符

[root@vm1 test]# echo Caiyun >> test2.txt

[root@vm1 test]# echo 791111890 >> test2.txt       

[root@vm1 test]# grep -n "[^0-9]" test2.txt

1:Caiyun

[-]   表示匹配"[-]"里一段字符的任意单个字符,如[0-9]即0到9

[root@vm1 test]# grep -n "[0-9]" text.txt   

8:My qq is 791111890

9:My mail is Caiyun111111@gmail.com.

1{4}     表示匹配字符 "1" 重复4次

[root@vm1 test]# grep -n "1{4}" text.txt

8:My qq is 791111890

9:My mail is Caiyun111111@gmail.com.

1{5,}    表示匹配字符 "1" 重复5次或5次以上

[root@vm1 test]# grep -n "1{5,}" text.txt 

9:My mail is Caiyun111111@gmail.com.

1{,6}    表示匹配字符 "1" 重复6次和6次以内

[root@vm1 test]# grep -n "1{,6}" text.txt 

1:My name is Caiyun.

2:I like badminton, snooker, running

3:

4:Maybe I'm not a smart person, but I'm working hard.

5:My blog is http://www.cnblogs.com/Caiyundo/

6:Welcome to my blog! CAIYUN

7:

8:My qq is 791111890

9:My mail is Caiyun111111@gmail.com.

1{3,5}  表示匹配字符 "1" 重复3-5次

[root@vm1 test]# grep -n "1{3,5}" text.txt     

8:My qq is 791111890

9:My mail is Caiyun111111@gmail.com.

二、扩展正则表达式

+     表示重复 "一个或一个以上" 前面的所有字符("*"是0个)

[root@vm1 test]# grep -n "11111*" text.txt   

8:My qq is 791111890

9:My mail is Caiyun111111@gmail.com.

[root@vm1 test]# grep -n "11111+" text.txt 

9:My mail is Caiyun111111@gmail.com.

?     表示重复 "0个或一个" 前面的字符("."是有且只有1个)

[root@vm1 test]# grep -n "11111." text.txt  

9:My mail is Caiyun111111@gmail.com.

[root@vm1 test]# grep -n "11111?" text.txt 

8:My qq is 791111890

9:My mail is Caiyun111111@gmail.com.

 |     表示过滤多个字符串

[root@vm1 test]# grep -nE "CAIYUN|mail" text.txt 

6:Welcome to my blog! CAIYUN

9:My mail is Caiyun111111@gmail.com.

[root@vm1 test]# grep -n "CAIYUN|mai" text.txt  

6:Welcome to my blog! CAIYUN

9:My mail is Caiyun111111@gmail.com.

一般情况下,当我们需要用到扩展正则表达式匹配符时,使用grep要加参数 ”-E” 或用 “” 转译符转义匹配符    

三、元字符

    边界字符,单词边界

[root@vm1 test]# grep 'Caiyun' text.txt  

My name is Caiyun.

My blog is http://www.cnblogs.com/Caiyundo/

My mail is Caiyun111111@gmail.com.

[root@vm1 test]# grep 'Caiyun' text.txt

My name is Caiyun.

grep的用法还有很多,只要符合规则、逻辑,还可以和很多其他命令配合使用。

PS:要活学活用,多用多练。

静静的学习一阵子儿...
原文地址:https://www.cnblogs.com/Caiyundo/p/8044179.html