grep基本用法
grep基本使用
	
过滤出/etc/passwd下前三行关于有nglogin的行、
	[root@centos7 ~]# grep -m3 nologin /etc/passwd
	bin:x:1:1:bin:/bin:/sbin/nologin
	daemon:x:2:2:daemon:/sbin:/sbin/nologin
	adm:x:3:4:adm:/var/adm:/sbin/nologin
	解释:
		-m
			显示n行的内容,这里的n是指定的多少行
			
过滤出/etc/fstab这个文件里不包含#的行
	[root@centos7 ~]# grep  -v ""# /etc/fstab 
	UUID=14d0e813-485c-41f0-9e43-e63033e31d93 /                       xfs     defaults        0 0
	UUID=1aae73b8-9d81-421f-9e24-4a6dd968e952 /boot                   xfs     defaults        0 0
	UUID=d70a2d8e-bff3-4c60-bbfb-313df0c3c6e6 swap                    swap    defaults        0 0
	解释:
		-v
			取反,也就是不包含的意思
			
过滤出/etc/passwd这个文件里包含root的行不区分大小写
	[root@centos7 ~]# grep -i ROOT /etc/passwd
	root:x:0:0:root:/root:/bin/bash
	operator:x:11:0:operator:/root:/sbin/nologin
	解释:
		-i
			不区分大小写
			
过滤出/etc/passwd下前三行关于有root的行并显示行号
	[root@centos7 ~]# grep -n root /etc/passwd
	1:root:x:0:0:root:/root:/bin/bash
	10:operator:x:11:0:operator:/root:/sbin/nologin
	解释:
		-n
			显示行号,也就是过滤出的内容在第几行
			
过滤出/etc/passwd这个文件里有多少行包含root的内容
	[root@centos7 ~]# grep -c root /etc/passwd
	2
	解释:	
		-c
			过滤出指定的内容有多少行
			
过滤出/etc/fstab这个文件里非注释的行并过滤出有多少行
	[root@centos7 ~]# grep -cv "#" /etc/fstab 
	4
	解释:	
		-c
			过滤出指定的内容有多少行
		
		-v
			取反,也就是不包含的意思
			
		"#"
			注释的#号
			
过滤出/etc/passwd里包含root的内容,只过滤出root,不包含其他字符
	[root@centos7 ~]# grep -o root /etc/passwd
	root
	root
	root
	root
	解释:
		-o
			过滤出指定的内容不包含其他字符
			
过滤出/etc/passwd下包含root的行并不显示出来
	[root@centos7 ~]# grep -q root /etc/passwd
	解释:
		-q
			静默模式,不显示输出内容


过滤出/etc/passwd这个文件里包含root的行和后面的三行内容
	[root@centos7 ~]# grep -A3 root /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
	adm:x:3:4:adm:/var/adm:/sbin/nologin
	--
	operator:x:11:0:operator:/root:/sbin/nologin
	games:x:12:100:games:/usr/games:/sbin/nologin
	ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
	nobody:x:99:99:Nobody:/:/sbin/nologin
	解释:
		-A
			取出指定内容后面的多少行,比如A3就是后面的三行
			

取出/etc/passwd里包含mail的行并显示前四行的内容
	[root@centos7 ~]# grep -B4 mail /etc/passwd
	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
	sync:x:5:0:sync:/sbin:/bin/sync
	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
	halt:x:7:0:halt:/sbin:/sbin/halt
	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin	
	解释
		-B
			取出指定内容的前面多少行的内容,比如B4就是前四行


取出/etc/passwd下包含mail,并取出mail前的三行和后面的三行的内容
	[root@centos7 ~]# grep -C3 mail /etc/passwd
	sync:x:5:0:sync:/sbin:/bin/sync
	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
	halt:x:7:0:halt:/sbin:/sbin/halt
	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
	operator:x:11:0:operator:/root:/sbin/nologin
	games:x:12:100:games:/usr/games:/sbin/nologin
	ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
	解释:
		-C
			取出指定内容的前后各多少行,比如-C3就是取出指定的内容的后面三行和后面的三行的内容
			
			
过滤出/etc/passwd里包含root和包含的mail的行
	[root@centos7 ~]# grep -e root -e mail /etc/passwd
	root:x:0:0:root:/root:/bin/bash
	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
	operator:x:11:0:operator:/root:/sbin/nologin
	解释:
		-e
			多个判断条件。包含多个内容就可以使用多个-e
			
取出包含root并包含bash的行的内容
	[root@centos7 ~]# grep -e root /etc/passwd|grep -e bash
	root:x:0:0:root:/root:/bin/bash
	

利用test.txt文件里的内容取出/etc/passwd/里包含test.txt文件里内容的行
	创建test.txt,添加root和bash两行内容
		[root@centos7 ~]# echo "root" >> test.txt
		[root@centos7 ~]# echo "bash" >> test.txt
	
	利用grep取出test.txt文件里的内容
		[root@centos7 ~]# grep -f test.txt /etc/passwd
		root:x:0:0:root:/root:/bin/bash
		operator:x:11:0:operator:/root:/sbin/nologin
		解释:
			-f
				指定文件,比如test.txt
				
利用grep取出两个文件相同的内容
	先来创建a.txt,并追加内容进去
		[root@centos7 ~]# echo "a" >> a.txt
		[root@centos7 ~]# echo "b" >> a.txt
		[root@centos7 ~]# echo "c" >> a.txt
		[root@centos7 ~]# echo "d" >> a.txt
		[root@centos7 ~]# echo "d" >> b.txt
		[root@centos7 ~]# echo "e" >> b.txt
		[root@centos7 ~]# echo "f" >> b.txt
		[root@centos7 ~]# echo "a" >> b.txt
		
	利用grep过滤出相同的内容
		[root@centos7 ~]# grep -f a.txt b.tx
		a
		解释:
			-f
				指定文件
				
只知道root的内容,但是不知道在哪个文件里
	[root@centos7 ~]# grep -r root /etc/*
	。。。。。。。。
	解释:
		-r
			递归目录,不取出有软连接的文件
			
		-R
			递归目录。但处理软连接
			

	


本文 暂无 评论

Top