linux13 linux三剑客-grep和正则表达式

本文介绍了grep命令的基本用法,包括查找特定内容、行号、大小写忽略、行数统计、反向查找等,并详细讲解了正则表达式的概念及实战应用,如匹配数字、字符组合、重复次数等。通过实例演示了如何在实际场景中使用这些技术来过滤和分析文件内容。
摘要由CSDN通过智能技术生成

linux13 linux三剑客-grep和正则表达式

grep命令

1、linux三剑客之grep命令

	和find差不太多,他们俩的区别是find无法查找文件的内容
	grep可以查找内容等 筛选数据

	格式:
			grep [参数] [过滤的规则] [路径]
			
			标准输出 | grep [参数] [过滤规则]
		参数:
			-n : 显示过滤出来的文件在文件当中的行号
			-o : 只显示筛选到的内容(比如筛选的root,只会显示root)
			-q : 静默输出(不输出的意思,一般用来shell脚本当中)
				可以echo $? 输出,看结果是不是0,0成功,非0失败
			-i : 忽略规则的大小写
			-c : 统计过滤出内容的行数
			-v :反向查找 ,不包含过滤规则的 也可以说是排除,反转查找
			-w : 匹配某个词 ,精确匹配
			-E :使用扩展正则 或者直接egrep
			-R :递归查询  可以找目录下,那些文件包含过滤规则(),打印文件路径
			-l : 只打印文件路径
				wc -l : 打印显示当前路径总共有多少行
		过滤规则:
			"这里写的是规则比如查找的是:root"
		扩展参数:(知道就好)
			grep -n -A 2 "mail" /etc/passwd
			-A :显示匹配到的数据的后n行
			-B :显示匹配到的数据的前n行
			-C :显示匹配到的数据的前后各n行
			
案例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 tmp]# grep -o "root" /etc/passwd
    root
    root
    root
    root
案例3:要求过滤/etc/passwd中的Root,忽略大小写
	[root@localhost tmp]# 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 tmp]# 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 tmp]# 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 tmp]# 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 tmp]# grep -c "root" /etc/passwd
	2	
案例8:要求查询不包含root的行
	[root@localhost tmp]# grep -v "root" /etc/passwd
	
案例9:匹配zhang这个词
	[root@localhost tmp]# grep -w "zhang" /tmp/1.txt 
	zhang:shi
案例10:要求匹配出包含yang的行
	[root@localhost tmp]# cat 1.txt 
    zhang:shi
    zhangshi
    zhangsu
    zzzhang
    zhangzhangzhangs1
    [root@localhost tmp]# grep -E "(zhang)+" /tmp/1.txt 
    zhang:shi
    zhangshi
    zhangsu
    zzzhang
    zhangzhangzhangs1
案例11:要求找出/etc目录下,那些文件中包含root
	[root@localhost tmp]# grep -R "root" /etc/	

练习1:计算/etc目录下包含root的文件有多少个?
	[root@localhost tmp]# grep -Rl "root" /etc | wc -l
	146
			
练习2:查询/etc/passwd文件中包含/bin/bash的行并输出行号
	[root@localhost tmp]# grep -n "\bin\bash" /etc/passwd
	上面的路径分隔符打错了要注意
    [root@localhost tmp]# grep -n "/bin/bash" /etc/passwd
    1:root:x:0:0:root:/root:/bin/bash
    21:test:x:1000:1000::/home/test:/bin/bash
    23:gailun:x:111:222::/home/gailun:/bin/bash
    24:test:x:996:995::/home/test:/bin/bash
    25:test666:x:124:123:手动创建用户:/home/test666:/bin/bash
    26:test111:x:1001:1001::/home/test111:/bin/bash

练习3:过滤出当前主机包含的IP
	[root@localhost tmp]# ip a | egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}"
									(0-9 最少有一个 最多三个在跟着一个.就成了192.)这些有三个
									就成了xxx.xxx.xxx.最后一个0-9 最少一个最多三个

2、正则表达式

	
	正则表达式是通过包含特殊含义的一些字符去适配各种匹配场景,从而匹配出我们想要的结果
	比如 手机号,邮箱,身份证等

	1、普通正则表达式
		前导字符:参数的前一个字符
		
		*			:匹配零个或多个前导字符,
			grep "ifcfg*" /tmp   
             grep的前面必须是ifcf  ,g可以是0个也可以多个这个*可以匹配0个或多个
			与find的*有点不同,,而find /tmp/ -name  "ifcfg*"  只能匹配有ifcfg的名字 
		$ 			:以前导字符结尾,以什么什么结尾
		.			:匹配任意一个字符(换行符除外)
		^			:以前导字符开头的行^写在前面
		[^]			:取反 等同于排除 
		.*			:所有的字符的任何个数  
			.后面的任意字符,比如n.* n后面不管有什么都匹配上 
			和n*对比,n*是只匹配n后面的n 其他值都不匹配
		[]			: 或者(其中包含的所有的字符的或者)
		不同情况下,代表不一样
		[a-z]		:a-z所有的一个字母
		[A-Z]		:A-Z所有的一个字母
		[0-9]		:0-9所有的一个数字	

	案例1:匹配包含22的行
		[root@localhost tmp]# grep "22" 2.txt 	
	案例2:以3结尾的行
		[root@localhost tmp]# grep "3$" 2.txt 
	案例3:要求输出包含eth的行
		[root@localhost tmp]# grep "eth" 1.txt 
	案例4:以1开头的行
		[root@localhost tmp]# grep "^1" 2.txt 
	案例5:要求打印出/etc/nginx/nginx.conf中的不是以#开头的行
		[root@localhost tmp]# grep "^[^#]" /etc/nginx/nginx.conf
	案例6:要求匹配出2后面有任意数量的任意字符
		[root@localhost tmp]# grep "2.*" 2.txt 
	案例7:要求匹配出本机中所有的普通用户
		[root@localhost tmp]# grep ":[0-9][0-9][0-9][0-9]" /etc/passwd
		加:是为了防止用户名也是四位的:四位数肯定是普通用户的uid了

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

egrep  等价于  grep -E 

		+			: 前导字符的一个或多个  *是0个或多个
		?			: 前导字符的零个或一个
		|			:  或者(竖线两边的字符的或者)
		()			: 分组,组成一个整体	
			\n      :  n代表的是前面第几个分组
		{m,n}		:  范围,至少有m个,最多有n个
			{m}		:  范围,固定m个
			{m,}	: 范围,至少有m个
		^$          : 表示空行
记得加-E 或者egrep

    案例1:匹配一个或多个2
		[root@localhost tmp]# egrep "2+" 2.txt 
		或者
		[root@localhost tmp]# egrep "22*" 2.txt
		或者
		[root@localhost tmp]# egrep "2{1,}" 2.txt
    案例2:查询出3个2
		[root@localhost tmp]# egrep "2{3}" 2.txt 
		或者
		[root@localhost tmp]# egrep "22." 2.txt 
    案例3:查询出包含 22 或者 33 的行
		[root@localhost tmp]# egrep "22|33" 2.txt 
    案例4:匹配出包含12341234的行
		[root@localhost tmp]# egrep "12341234" 2.txt 
		或者
		[root@localhost tmp]# egrep "(1234)\1" 2.txt 
		或者
		[root@localhost tmp]# egrep "(1234){2}" 2.txt 
    案例5;要求匹配出包含1234abcd1234abcd的行
		[root@localhost tmp]# egrep "(1234)(abcd)\1\2" 2.txt 

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

[root@localhost tmp]# ip a | egrep -o  "([0-9]{1,3}\.){3}[0-9]{1,3}" 
127.0.0.1
192.168.15.222
192.168.15.255
172.16.1.100
172.16.15.255	

练习2:过滤出手机号

[root@localhost tmp]# echo 15112345678 | egrep "(151|152|131|154)[0-9]{8}"
15112345678

练习3:过滤出邮箱

[root@localhost tmp]# echo 1617271856@qq.com | egrep "[0-9A-Za-z]+@[0-9a-z]+(\.com|\.cn)"
1617271856@qq.com	





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值