今天内容

1、linux三剑客之grep命令

1、grep命令的作用和使用场景

grep命令一般用来筛选数据,一般用来筛选我们需要的数据。

格式:

grep [参数] [过滤的规则] [路径]

 

            标准输出 | grep [参数] [过滤规则]

案例:

案例1:要求找出/etc目录下,那些文件中包含root

案例2:要求输出本机的所有IP

2、grep命令的参数

参数:

-n : 显示过滤出来的文件在文件当中的行号
-o : 只显示匹配到的内容
-q : 静默输出(一般用来shell脚本当中)
-i : 忽略大小写
-c : 显示匹配到的行数
-v :反向查找
-w : 匹配某个词
-E :使用扩展正则
-R :递归查询
-l : 只打印文件路径

 

        扩展参数:

-A :显示匹配到的数据的后几n行
-B :显示匹配到的数据的前几n行
-C :显示匹配到的数据的前后各几n行

知识储备:

$? : 代表上一条命令执行是否成功(0:成功,非0代表失败)
词 :一连串字母和数字组成的字符串
+   :
wc -l : 打印显示有多行

 

        案例

案例1:要求过滤出/etc/passwd中包含的root的行及其行号

[root@localhost ~]# grep -n "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

案例2:要求过滤出/etc/passwd中包含的root的行,只显示过滤到的内容

[root@localhost ~]# grep -o "root" /etc/passwd
root
root
root
root

案例3:要求过滤/etc/passwd中的Root,忽略大小写

[root@localhost ~]# grep -i 'Root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

案例4:要求匹配mail及其后两行

[root@localhost ~]# grep -n -A 2 "mail" /etc/passwd
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10-operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin

案例5:要求匹配mail及其前两行

[root@localhost ~]# grep -n -B 2 "mail" /etc/passwd
7-shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8-halt:x:7:0:halt:/sbin:/sbin/halt
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

案例6:要求匹配mail及其前后各两行

[root@localhost ~]# grep -n -C 2 "mail" /etc/passwd
7-shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8-halt:x:7:0:halt:/sbin:/sbin/halt
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10-operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin

案例7:要求显示包含root的行有多少行
[root@localhost ~]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# grep -c "root" /etc/passwd
2

案例8:要求查询不包含root的行

[root@localhost ~]# grep -v "root" /etc/passwd

案例9:匹配yang这个词

[root@localhost ~]# cat 1.txt
12e2341324
dfsdfgsd
oldboy
shanhe
yang:chen
yangchen
[root@localhost ~]# grep -w 'yang' 1.txt
yang:chen

案例10:要求匹配出包含yang的行

[root@localhost ~]# cat 1.txt
12e2341324
dfsdfgsd
oldboy
shanhe
yang:chen
yangchen
yangyangyang
[root@localhost ~]# grep -E "(yang)+" 1.txt
yang:chen
yangchen
yangyangyang

案例11:要求找出/etc目录下,那些文件中包含root

[root@localhost ~]# grep -R "root" /etc/

只打印文件路径
[root@localhost ~]# grep -Rl "root" /etc/

 

        练习

练习1:计算/etc目录下包含root的文件有多少个?

[root@localhost etc]# grep -R -l "root" /etc/ | wc -l
145

练习2:查询/etc/passwd文件中包含/bin/bash的行并输出行号

[root@localhost etc]# grep -n "/bin/bash" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
22:test02:x:1001:1001::/home/test02:/bin/bash
23:test03:x:1002:1002::/home/test03:/bin/bash

练习3:过滤出当前主机包含的IP

[root@localhost etc]# ip a | grep -o -E "([0-9]{1,3}\.){3}([0-9]){1,3}"
127.0.0.1
192.168.15.50
192.168.15.255
172.16.1.50
172.16.15.255

 

    3、grep命令和正则表达式的结果

2、正则表达式

正则表达式是通过包含特殊含义的一些字符去适配各种匹配场景,从而匹配出我们想要的结果

例如:

1、怎样验证用户输入的手机号?

2、怎样验证用户输入的邮箱?

3、怎样验证用户输入的身份证号码?

1、普通正则表达式

* :匹配零个或多个前导字符
$ :以前导字符结尾
. :匹配任意一个字符(换行符除外)
^ :以前导字符开头的行
[^] :取反
.* :所有的字符的任何个数
[] : 或者(其中包含的所有的字符的或者)

 

        [a-z]       :a-z所有的一个字母
[A-Z] :A-Z所有的一个字母
[0-9] :0-9所有的一个数字

 

    

 

        案例1:匹配包含22的行

[root@localhost ~]# grep "222?" 1.txt
1111122223333
        22223333 .*
    2222   2*
    222               2?

案例2:以3结尾的行
[root@localhost ~]# grep -E "33$" 1.txt
1111122223333

案例3:要求输出包含eth的行

[root@localhost ~]# grep "eth." 2.txt
/etc/sysconfig/network-scripts/ifcfg-eth1
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/ifcfgg-eth0

案例4:以1开头的行

[root@localhost ~]# cat 1.txt
1111122223333
123
13
1234
234
345
456
[root@localhost ~]# grep "^1" 1.txt
1111122223333
123
13
1234

案例5:要求打印出/etc/nginx/nginx.conf中的不是以#开头的行

[root@localhost ~]# grep "^[^#]" /etc/nginx/nginx.conf

 

        案例6:要求匹配出2后面有任意数量的任意字符

[root@localhost ~]# grep "2." 1.txt
1111122223333

 

        案例7:要求匹配出本机中所有的普通用户

[root@localhost ~]# grep ":[0-9][0-9][0-9][0-9]" /etc/passwd
test02:x:1001:1001::/home/test02:/bin/bash
test03:x:1002:1002::/home/test03:/bin/bash

 

 

 

 

    2、扩展正则表达式(grep 必须加-E参数,或者使用egrep命令)

egrep 等价于 grep -E

+ : 前导字符的一个或多个
? : 前导字符的零个或一个
| : 或者(竖线两边的字符的或者)
() : 分组,组成一个整体
\n     : n代表的是前面第几个分组
{m,n} : 范围,至少有m个,最多有n个
{m} : 范围,固定m个
{m,} : 范围,至少有m个

3、案例

案例1:匹配一个或多个2

[root@localhost ~]# egrep "2+" 1.txt
1111122223333
123
1223
1234
234
2

[root@localhost ~]# egrep "22*" 1.txt
1111122223333
123
1223
1234
234
2

[root@localhost ~]# egrep "2{1,}" 1.txt
1111122223333
123
1223
1234
234
2

案例2:查询出3个2

[root@localhost ~]# egrep "2{3}" 1.txt
1111122223333

案例3:查询出包含 22 或者 33 的行

[root@localhost ~]# egrep "22|33" 1.txt
1111122223333
1223
13333

案例4:匹配出包含12341234的行

[root@localhost ~]# grep "12341234" 1.txt
123412341234

[root@localhost ~]# grep -E "(1234){2}" 1.txt
123412341234

[root@localhost ~]# grep -E "(1234)\1" 1.txt
123412341234

案例5;要求匹配出包含1234abcd1234abcd的行

[root@localhost ~]# grep "1234abcd1234abcd" 1.txt
1234abcd1234abcd1234abcd
[root@localhost ~]# grep -E "(1234)(abcd)\1\2" 1.txt
1234abcd1234abcd1234abcd

 

    练习:

练习1:过滤出当前系统IP

[root@localhost ~]# ip a | grep -o -E "([0-9]{1,3}\.){3}[0-9]{1,3}"
127.0.0.1
192.168.15.50
192.168.15.255
172.16.1.50
172.16.15.255

 

        练习2:过滤出手机号

[root@localhost ~]# echo "15516134567" | grep -E "(155|187|136|166|177|138|137|188)[0-9]{8}"
15516134567
[root@localhost ~]# echo "25516134567" | grep -E "(155|187|136|166|177|138|137|188)[0-9]{8}"
        练习3:过滤出邮箱

[root@localhost ~]# echo "12345@qq.com" | grep -E "[0-9a-zA-Z]+@[0-9a-z]+(\.com|\.com\.cn|\.edu|\.edu\.cn|\.top)"
12345@qq.com
[root@localhost ~]# echo "chenyangqit@qq.com" | grep -E "[0-9a-zA-Z]+@[0-9a-z]+(\.com|\.com\.cn|\.edu|\.edu\.cn|\.top)"
chenyangqit@qq.com
[root@localhost ~]# echo "chenyangqit@qq.edu" | grep -E "[0-9a-zA-Z]+@[0-9a-z]+(\.com|\.com\.cn|\.edu|\.edu\.cn|\.top)"
chenyangqit@qq.edu
[root@localhost ~]# echo "chenyangqit@qq.edu.cn" | grep -E "[0-9a-zA-Z]+@[0-9a-z]+(\.com|\.com\.cn|\.edu|\.edu\.cn|\.top)"
chenyangqit@qq.edu.cn

 

 

 

posted on 2021-09-29 15:41  李辉111  阅读(73)  评论(0编辑  收藏  举报