shell编程之文本三剑客——grep

1.grep介绍

grep 过滤  通用的正则表达式分析程序
   grep、egrep、fgrep
   做匹配来过滤的
grep [options] pattern [files]
或
grep [-abcEFGhHilLnqrsvVwxy][-A<显示行数>][-B<显示列数>][-C<显示列数>][-d<进行动作>][-e<范本样式>][-f<范本文件>][--help][范本样式][文件或目录...]
pattern - 表示要查找的字符串或正则表达式。
files - 表示要查找的文件名,可以同时查找多个文件,如果省略 files 参数,则默认从标准输入中读取数据。

用途:在文件中查找并显示包含指定字符串的行
格式:grep  [选项]...  模式  目标文件
-i:查找时忽略大小写
-v:反转查找,输出与模式不相符的行 
-n:显示符合模式要求的行号
-r:递归搜索所有文件
-o:只显示匹配的内容
-E: 支持更多的元字符(支持扩展正则)
-A: 找到匹配行以及后几行
-B:输出匹配行以及前几行

如果觉得这些命令少了, 可以man grep看其他的选项

 

2.grep选项的运用

# -i:查找时忽略大小写
# 接-i选项会把每行有port全查出来忽略大小写, 单词里面有port的也会查
[root@test ~]# grep -i "port"   /etc/ssh/sshd_config 
# If you want to change the port on a SELinux system, you have to tell
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#Port 22
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
#GatewayPorts no

# -v:反转查找,输出与模式不相符的行 
# 这里就是把没有d字母的行全查找出来
[root@test shell-test]# cat  a.txt 
aa bb cc
a2 b2 c2 d2
a3 b3 c3
[root@test shell-test]# grep -v "d" a.txt
aa bb cc
a3 b3 c3


# -n:显示符合模式要求的行号
# 可以多选项操作
[root@test shell-test]# grep -nv "d" a.txt
1:aa bb cc
3:a3 b3 c3

# -r:递归搜索所有文件
[root@test test_dir]# cat test.txt 
test

test.py
[root@test test_dir]# echo "test66" >>test2.txt 
[root@test test_dir]# echo "test555" >>test2.txt 
[root@test test_dir]# grep -r  "test" .    # .是当前文件
./test.txt:test
./test.txt:test.py
./test2.txt:test66
./test2.txt:test555

# -o:只显示匹配的内容
[root@test test_dir]# grep -or  "test" .
./test.txt:test
./test.txt:test
./test2.txt:test
./test2.txt:test

# -E: 支持更多的元字符(支持扩展正则) 用egerp 一样的效果 
# 查找以aa开头的行
[root@test shell-test]# grep -E "^aa" a.txt 
aa bb cc
[root@test shell-test]# egrep "^aa" a.txt 
aa bb cc

# -A: 找到匹配行以及后几行
# -B:输出匹配行以及前几行
[root@test shell-test]# egrep -A 2 "^aa" a.txt 
aa bb cc
a2 b2 c2 d2
a3 b3 c3

[root@test shell-test]# egrep -B 2 "^a3" a.txt 
aa bb cc
a2 b2 c2 d2
a3 b3 c3

 

3.简单正则

#  ^aa  以aa开头的行
#  aa$  表示以aa结尾的行
# [] 表示一个字符集
  [a-z] 从a-z中取一个
  [^a-z] 不取a-z的字符
  ^[^a-zA-Z0-9_]  显示不以字母、数字、下划线开头的行
  -v ^[a-zA-Z0-9_]  

# 通配符
  * 代表匹配前一个项任意次  0-n
  ? 代表匹配前一个项0次或者1次 0,1
  + 代表匹配前一个项一次到多次  1-n
  . 占位符 除\n之外的任意字符
  {n,m} 匹配前一项n到m次


# \b匹配单词的边界  \b.*\b
# \B不匹配单词边界
# \w 匹配单词字符(a-z, A-Z, 0-9, 中文)
# \W 不匹配单词字符
# \s 匹配空白字符(空格,tab)
# \S 匹配非空白字符

4.练习

4.1.题目 

#1、复制/etc/passwd到当前目录下,然后对passwd进行操作,提前新建liu姓的用户
#2、查找出当前passwd文件中以ftp或者mail开头的行,输出到屏幕
#3、查找出当前passwd文件中不以r、m、f开头的行
#4、查找出当前passwd中以bash结尾的行
#5、查找出/etc/login.defs文件中的有效行(不显示空行和注释行,以#号开头的行)
#6、查找出/var/log/messages 文档中有15个字母的单词
#7、查找出/etc/passwd文件里用户名包含liu同时使用bash的用户
#8、查找出/etc/ssh/sshd_config 文件里包含连续2个字符的行
#9、查找出包含特殊字符的行
#10、查找出不包含数字的行
#11、查找出/var/log/secure里的ip地址

 4.2 1-5题

[root@test shell-test]# cp /etc/passwd  .
cp:是否覆盖"./passwd"? y

#2、查找出当前passwd文件中以ftp或者mail开头的行,输出到屏幕
[root@test shell-test]# grep  -E  "^ftp|^mail"  passwd 
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin

# 3、查找出当前passwd文件中不以r、m、f开头的行
[root@test shell-test]# egrep  -v "^r|^m|^f"  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
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync

[root@test shell-test]# grep -E   "^[^rmf]"  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
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync

# 4、查找出当前passwd中以bash结尾的行
[root@test shell-test]# egrep "bash$" passwd 
root:x:0:0:root:/root:/bin/bash
gala:x:1000:1000::/home/gala:/bin/bash
liuliu:x:1001:1001::/home/liuliu:/bin/bash
haoliu:x:1002:1002::/home/haoliu:/bin/bash
sc:x:1003:1003:liliu:/home/sc:/bin/bash

# 5、查找出/etc/login.defs文件中的有效行(不显示空行和注释行,以#号开头的行)
[root@test shell-test]# grep -vE "^#|^$" /etc/login.defs 
MAIL_DIR	/var/spool/mail
PASS_MAX_DAYS	99999
PASS_MIN_DAYS	0
PASS_MIN_LEN	5
PASS_WARN_AGE	7
UID_MIN                  1000
UID_MAX                 60000
SYS_UID_MIN               201
SYS_UID_MAX               999
GID_MIN                  1000
GID_MAX                 60000
SYS_GID_MIN               201
SYS_GID_MAX               999
CREATE_HOME	yes
UMASK           077
USERGROUPS_ENAB yes
ENCRYPT_METHOD SHA512

[root@test shell-test]# egrep "^[^#$]" /etc/login.defs 
MAIL_DIR	/var/spool/mail
PASS_MAX_DAYS	99999
PASS_MIN_DAYS	0
PASS_MIN_LEN	5
PASS_WARN_AGE	7
UID_MIN                  1000
UID_MAX                 60000
SYS_UID_MIN               201
SYS_UID_MAX               999
GID_MIN                  1000
GID_MAX                 60000
SYS_GID_MIN               201
SYS_GID_MAX               999
CREATE_HOME	yes
UMASK           077
USERGROUPS_ENAB yes
ENCRYPT_METHOD SHA512

 4.3 6-10题

# 6.查找出/var/log/messages 文档中有15个字母的单词
[root@test shell-test]# egrep -i "\b[a-z]{15}\b" /var/log/messages
Sep 27 11:30:18 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 14:22:52 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 15:12:01 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 15:23:11 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 16:14:41 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 16:16:52 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 16:36:43 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 18:59:15 test kernel: Booting paravirtualized kernel on VMware hypervisor

# [a-Z]  == [a-zA-Z] linux里面是以字典排序,而python是ascii码,所以python不能用[a-Z].
# 因为不相邻
[root@test shell-test]# grep -E "\b[a-Z]{15}\b" /var/log/messages
Sep 27 11:30:18 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 14:22:52 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 15:12:01 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 15:23:11 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 16:14:41 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 16:16:52 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 16:36:43 test kernel: Booting paravirtualized kernel on VMware hypervisor
Sep 27 18:59:15 test kernel: Booting paravirtualized kernel on VMware hypervisor

# 7、查找出/etc/passwd文件里用户名包含liu同时使用bash的用户
# 简单的正则不需要接-E也能用
[root@test shell-test]# egrep "^[^:]*liu[^:]*:" passwd |grep  bash$ 
liuliu:x:1001:1001::/home/liuliu:/bin/bash
haoliu:x:1002:1002::/home/haoliu:/bin/bash
haoliua:x:1004:1004::/home/haoliua:/bin/bash

[root@test shell-test]# egrep "^[0-Z_]*liu[0-Z_]*:" passwd |grep  bash$ 
liuliu:x:1001:1001::/home/liuliu:/bin/bash
haoliu:x:1002:1002::/home/haoliu:/bin/bash
haoliua:x:1004:1004::/home/haoliua:/bin/bash


# 8、查找出/etc/ssh/sshd_config 文件里包含连续2个字符的行
[root@test shell-test]# grep -E "(.)\1" /etc/ssh/sshd_config 


# 9.查找出包含特殊字符的行
[root@test shell-test]# egrep  "[^0-Z]" /etc/ssh/sshd_config 
#Match User anoncvs
#	X11Forwarding no
#	AllowTcpForwarding no
#	PermitTTY no
#	ForceCommand cvs server


# 10. 查找出不包含数字的行
[root@test shell-test]# cat a.txt 
aa bb cc
a2 b2 c2 d2
a3 b3 c3

[root@test shell-test]# egrep -v "[0-9]"  a.txt 
aa bb cc




4.4.11题

# 查找出/var/log/secure里的ip地址
用列举法一个个列出来,注意.占位符 除\n之外的任意字符所以要\.来表示
"((((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9][0-9])|([0-9]))\.){3})((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9][0-9])|([0-9]))"
[root@test shell-test]# grep -oE "((((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9][0-9])|([0-9]))\.){3})((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9][0-9])|([0-9]))" /var/log/secure
0.0.0.0
192.168.249.1
0.0.0.0
192.168.249.1
0.0.0.0
192.168.249.1
0.0.0.0
192.168.249.1
0.0.0.0
192.168.249.1
0.0.0.0
192.168.249.1
0.0.0.0
192.168.249.1
0.0.0.0
192.168.249.1

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值