博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
正则介绍及grep/egrep用法
阅读量:6952 次
发布时间:2019-06-27

本文共 10235 字,大约阅读时间需要 34 分钟。

hot3.png

10月16日任务

9.1 正则介绍_grep上

9.2 grep中

9.3 grep下

扩展

把一个目录下,过滤所有*.php文档中含有eval的行

grep -r --include="*.php" 'eval' /data

 

9.1 正则介绍_grep上

什么是正则

  • 正则就是一串有规律的字符串

  • 掌握好正则对于编写shell脚本有很大帮助

  • 各种编程语言中都有正则,原理是一样的

  • 本章将要学习grep/egrep、sed 、awk   三剑客

#Cent OS 7.0以下的版本grep和egrep是不带颜色显示的,需要手动添加alias。

[root@centos6 ~]# vim .bashrc [root@centos6 ~]# . .bashrc [root@centos6 ~]# which egrepalias egrep='egrep --color=auto'    /bin/egrep[root@centos6 ~]# which grepalias grep='grep --color=auto'    /bin/grep

 

grep

  • grep [-cinvABC] 'word' filename

  • -c    行数

  • -i     不区分大小写

  • -n     显示行号

  • -v     取反

  • -r     遍历所有子目录

  • -A   后面跟数字,过滤出符合要求的行以及下边的n行

  • -B     同上,过滤出符合要求的行以及上边的n行

  • -C     同上,同时过滤出符合要求的行以及上下各n行

 

[root@centos6 ~]# which grepalias grep='grep --color=auto'    /bin/grep[root@centos6 ~]# grep -c 'nologin' /etc/passwd16[root@centos6 ~]# grep -n 'nologin' /etc/passwd2:bin:x:1:1:bin:/bin:/sbin/nologin3:daemon:x:2:2:daemon:/sbin:/sbin/nologin4:adm:x:3:4:adm:/var/adm:/sbin/nologin5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin10:uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin11:operator:x:11:0:operator:/root:/sbin/nologin12:games:x:12:100:games:/usr/games:/sbin/nologin13:gopher:x:13:30:gopher:/var/gopher:/sbin/nologin14:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin15:nobody:x:99:99:Nobody:/:/sbin/nologin16:vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin17:saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin18:postfix:x:89:89::/var/spool/postfix:/sbin/nologin19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin20:ntp:x:38:38::/etc/ntp:/sbin/nologin[root@centos6 ~]# grep -nv 'nologin' /etc/passwd1:root:x:0:0:root:/root:/bin/bash6:sync:x:5:0:sync:/sbin:/bin/sync7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown8:halt:x:7:0:halt:/sbin:/sbin/halt[root@centos6 ~]# grep -B2 'root' /etc/passwdroot:x:0:0:root:/root:/bin/bash--mail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologin[root@centos6 ~]# grep -A2 'root' /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin--operator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologin[root@centos6 ~]# grep -C1 'root' /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin--uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologin

9.2、grep (中)

grep/egrep示例

  • grep -n 'root' /etc/passwd

  • grep -nv 'nologin'   /etc/passwd

  • grep '[0-9]' /etc/inittab

  • grep -v '[0-9]'  /etc/inittab

  • grep -v '^#' /etc/inittab

  • grep -v '^#' /etc/inittab -v '^$'

  • grep '^[^a-zA-Z]'  text.txt             # ^在方括号外表示以什么开头,在方括号内表示取非

  • grep 'r.o' test.txt                           # .表示一个字符

  • grep 'oo*' test.txt                          # *表示*左边的字符重复0-n次

  • grep '.*'    test.txt                          # .*表示匹配任意字符包括空行

  • grep 'o\{2\}' /etc/passwd               # 特殊符号需要用脱意符号\,不然无法识别

  • egrep 'o{2}' /etc/passwd                 # 用egrep命令,不需要脱意,{}表示前边字符的重复范围

  • egrep 'o+' /etc/passwd                  # +表示+左边字符重复1-n次

  • egrep 'oo?' /etc/passwd                 # ?表示?前边的字符重复0或者1次

  • egrep 'root|nologin' /etc/passwd    # |表示或者

  • egrep '(oo){2}'  /etc/passwd

[root@centos6 ~]# grep '[0-9]' passwd root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinsaslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinntp:x:38:38::/etc/ntp:/sbin/nologin[root@centos6 ~]# grep -v '[0-9]' passwd qwwwewer[root@centos6 ~]# vim inittab [root@centos6 ~]# grep -nv '#' inittab 26:aaaaaaaaaaaa27:ashdfoaoiifuoa28:11111111111111130:id:3:initdefault:[root@centos6 ~]# grep -v '#' inittab aaaaaaaaaaaaashdfoaoiifuoa111111111111111id:3:initdefault:[root@centos6 ~]# grep -n '#' inittab 1:# inittab is only used by upstart for the default runlevel.2:#3:# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.4:#5:# System initialization is started by /etc/init/rcS.conf6:#7:# Individual runlevels are started by /etc/init/rc.conf8:#9:# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf10:#11:# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,12:# with configuration in /etc/sysconfig/init.13:#14:# For information on how to write upstart event handlers, or how15:# upstart works, see init(5), init(8), and initctl(8).16:#17:# Default runlevel. The runlevels used are:18:#   0 - halt (Do NOT set initdefault to this)19:#   1 - Single user mode20:#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)21:#   3 - Full multiuser mode22:#   4 - unused23:#   5 - X1124:#   6 - reboot (Do NOT set initdefault to this)25:# 29:(&*&^$^$^#%#[root@centos6 ~]# grep -n '^#' inittab 1:# inittab is only used by upstart for the default runlevel.2:#3:# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.4:#5:# System initialization is started by /etc/init/rcS.conf6:#7:# Individual runlevels are started by /etc/init/rc.conf8:#9:# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf10:#11:# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,12:# with configuration in /etc/sysconfig/init.13:#14:# For information on how to write upstart event handlers, or how15:# upstart works, see init(5), init(8), and initctl(8).16:#17:# Default runlevel. The runlevels used are:18:#   0 - halt (Do NOT set initdefault to this)19:#   1 - Single user mode20:#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)21:#   3 - Full multiuser mode22:#   4 - unused23:#   5 - X1124:#   6 - reboot (Do NOT set initdefault to this)25:# [root@centos6 ~]# grep -n '^[0-9]' inittab 28:111111111111111[root@centos6 ~]# grep -n '^[^0-9]' inittab 1:# inittab is only used by upstart for the default runlevel.2:#3:# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.4:#5:# System initialization is started by /etc/init/rcS.conf6:#7:# Individual runlevels are started by /etc/init/rc.conf8:#9:# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf10:#11:# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,12:# with configuration in /etc/sysconfig/init.13:#14:# For information on how to write upstart event handlers, or how15:# upstart works, see init(5), init(8), and initctl(8).16:#17:# Default runlevel. The runlevels used are:18:#   0 - halt (Do NOT set initdefault to this)19:#   1 - Single user mode20:#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)21:#   3 - Full multiuser mode22:#   4 - unused23:#   5 - X1124:#   6 - reboot (Do NOT set initdefault to this)25:# 26:aaaaaaaaaaaa27:ashdfoaoiifuoa29:(&*&^$^$^#%#30:id:3:initdefault:

 

9.3、grep(下)

[root@centos6 ~]# grep 'r.o' passwd root:x:0:0:root:/root:/bin/bashahkdhfa:r>oo:dfsa:13243operator:x:11:0:operator:/root:/sbin/nologin[root@centos6 ~]# grep 'ntp.*nologin' passwd        #匹配以ntp开头,nologin结束的行ntp:x:38:38::/etc/ntp:/sbin/nologin[root@centos6 ~]# grep '{2}' passwd             #错误示范[root@centos6 ~]# grep 'o{2}' passwd           #错误示范[root@centos6 ~]# grep 'o\{2\}' passwd           # {}需要加脱意符号\root:x:0:0:root:/root:/bin/bashahkdhfa:r>oo:dfsa:13243lp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologin

 

#这样操作起来特别麻烦,如何简单操作呢,我们可以用到egrep命令了,或者grep -E

[root@centos6 ~]# egrep 'o{2}' passwd root:x:0:0:root:/root:/bin/bashahkdhfa:r>oo:dfsa:13243lp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologin[root@centos6 ~]# grep -E 'o{2}' passwd root:x:0:0:root:/root:/bin/bashahkdhfa:r>oo:dfsa:13243lp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologin[root@centos6 ~]# egrep 'o+t' passwd root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin[root@centos6 ~]# egrep 'o?t' passwd root:x:0:0:root:/root:/bin/bashshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltoperator:x:11:0:operator:/root:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinsaslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinntp:x:38:38::/etc/ntp:/sbin/nologin[root@centos6 ~]# egrep 'o?1o' passwd bin:x:1o:1:bin:/bin:/sbin/nologin[root@centos6 ~]# grep -E 'root|nologin' passwd root:x:0:0:root:/root:/bin/bashbin:x:1o:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinsaslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinntp:x:38:38::/etc/ntp:/sbin/nologin

转载于:https://my.oschina.net/u/3959708/blog/2246901

你可能感兴趣的文章
opencv提供的带参数例程
查看>>
Java跨域问题以及如何使用Cors解决前后端 分离部署项目所遇到的跨域问题
查看>>
尝试写作:第一天
查看>>
python集成包地址 Anaconda 一键安装拥有所有包
查看>>
SEO—搜索引擎优化初探
查看>>
使用宝塔控制面板建站时出现网页出现404错误怎么办?
查看>>
Confluence 6 附件存储配置
查看>>
Confluence 6 附件存储提取文本文件
查看>>
两种方式设置单元格的下划线
查看>>
解析:百度快照与站点权重的关系!
查看>>
实验吧 隐写
查看>>
redis_学习_02_redis 可视化工具 Redis Desktop Manager
查看>>
mongo去重统计
查看>>
学好机器学习,这里有你想要的一切
查看>>
Docker中使用MySQL
查看>>
RDIFramework.NET V2.8版本 ━ 开发实例之产品管理(WinForm)
查看>>
nodejs与javascript中的aes加密
查看>>
内存溢出真实案例分析
查看>>
Jboot v2.0-rc.12 发布,优化细节问题
查看>>
3.JUC线程高级-同步容器 ConcurrentHashMap
查看>>