前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >9.3 grep(下)

9.3 grep(下)

作者头像
运维小白
发布2018-02-06 11:42:56
9550
发布2018-02-06 11:42:56
举报
文章被收录于专栏:运维小白运维小白

grep用法

  • grep 'r.o' test.txt
  • grep 'oo*' test.txt
  • grep '.*' test.txt
  • grep 'o{2}' /etc/passwd
  • egrep 'o{2}' /etc/passwd
  • egrep 'o+' /etc/passwd
  • egrep 'oo?' /etc/passwd
  • egrep 'root|nologin' /etc/passwd
  • egrep '(oo){2}' /etc/passwd

grep命令 'r.o'

  • grep 'r.o' passwd 匹配出包含r 和o 的字符
    • 其中的 . 表示匹配任意的一个字符,只能是一个字符
    • 这个点可以是字符,英文字母,数字,特殊符号(包括 . )
[root@hf-01 grep]# grep 'r.o' passwd    //会匹配出包含ro的字符,其中的点表示任意一个字符
[root@hf-01 grep]# grep 'r.o' passwd
root:x:0:0:root:/root:/bin/bash
adas:124:bdsf:rto:pass
halt:x:7:0:halt:/r.o:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin

grep命令 'r*o'

  • grep 'h*n' passwd //表示匹配星号左边的h字符,重复0到N次
    • 星号* ,表示* 前面需要有一个字符 表示0个或多个*前面的字符 跟后面有啥没有关系
    • h* 可以是啥都没有啊(包括0个h)
    • h*n 只要有n就匹配
[root@localhost grep]# grep 'h*n' inittab
# inittab is no longer used when using systemd.
hhhhan
haaaaannnn
wannnnnnn
# Ctrl-Alt-Delete is handled by /etc/systemd/system/ctrl-alt-del.target
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
# To set a default target, run:
# ln -sf /lib/systemd/system/<target name>.target /etc/systemd/system/default.target
[root@hf-01 grep]# 

总结grep 'hn' passwd 过滤的时候,跟号前面的字符相关,可以是0个或多个,跟后面的字符没有什么关系,若是在一行中无 h 而有 n 也会匹配出来(这就表示0个h)

grep命令 '.*'

  • grep '.*' inittab // 会匹配任意个任意字符(包括空行)
    • 零个字符也可以匹配
  • 若想匹配某一行,则可以写成grep 'inittab.*' inittab
[root@localhost grep]# grep '# inittab.*' inittab
# inittab is no longer used when using systemd.

grep命令 'h{2}' 连续匹配两次h

  • grep 'h{2}' inittab //表示 h 连续出现两次匹配出来(只要是成对的 h 就都会匹配出来)
    • \ 表示脱义,若不加脱义符号,则无法匹配出来
[root@localhost grep]# grep 'h\{2\}' inittab
hhhhan
[root@localhost grep]# 
  • 在匹配的时候,加个范围'h{0,4}' 匹配0到 3次
  • grep 'h{0,4}' inittab //匹配
[root@localhost grep]# grep 'h\{0,4\}' inittab    //匹配文件中h出现0到4次的都会匹配出来
# inittab is no longer used when using systemd.
hhhhan
haaaaannnn
wannnnnnn
waaaaaal
# Ctrl-Alt-Delete is handled by /etc/systemd/system/ctrl-alt-del.target
#
等等等,只截取了一部分
  • 若不想加脱义符号,则可以使用
    • 方法一:grep -E 'h{2}' inittab 连续匹配h两次
    • 方法二:egrep 'h{2}' inittab 连续匹配h两次
[root@localhost grep]# grep -E 'h{2}' inittab
hhhhan

egrep命令 'h{2}' 连续匹配两次h

  • egrep 'h{2}' inittab 连续加匹配两次h
    • {}花括号表示前面的字符重复范围
[root@localhost grep]# egrep 'h{2}' inittab
hhhhan
[root@localhost grep]# 

grep命令 -E参数 '(hh){2}'

  • grep -E '(hh){2}' inittab //表示连续的hh出现两次匹配打印出来
[root@localhost grep]# grep -E '(hh){2}' inittab    //表示连续的hh出现两次匹配打印出来
hhhhan
  • 或者使用egrep命令
  • egrep '(hh){2}' inittab //表示连续的hh出现两次匹配打印出来
[root@localhost grep]# egrep '(hh){2}' inittab
hhhhan

grep -E等于egrep命令,若是在连续匹配字符的时候,不使用 grep -E或 egrep 命令,只使用grep命令,其中的符号则需要脱义。

grep命令 'h+h'

  • grep 'h+h' inittab
  • egrep 'h+h' inittab
    • +加号,表示加号前面的字符,一次或多次
      • 对比 号,区别是号是0次或多次
[root@localhost grep]# egrep 'h+h' inittab
hhhhan
[root@localhost grep]# egrep 'h+a' inittab
hhhhan
haaaaannnn
# Ctrl-Alt-Delete is handled by /etc/systemd/system/ctrl-alt-del.target
[root@localhost grep]# 

grep命令 'h?1f'

  • grep 'h?1f' inittab //匹配h和n字符,若h没有,则匹配1f字符
  • egrep 'h?1f' inittab
    • ?问号,表示?前面的字符重复次数为0或1
[root@localhost grep]# egrep 'h?1f' inittab    //匹配h和1f字符,若文件没有h字符,则匹配1f字符
h1f:gfdgfg
hghjhk:1f:hjjkuhhj
[root@localhost grep]# 

grep命令 'root|nologin'

  • grep 'h|1f' inittab //在文件中匹配出h或1f(可以同时出现h和1f)
  • egrep 'h|1f' inittab
    • | 竖线表示 或者 是意思
    • 可以连续匹配匹配,比如 egrep 'h|1f|use' inittab
    • 若想不区分大小写,则可以加 -i 参数
[root@localhost grep]# egrep 'h|1f' inittab
# inittab is no longer used when using systemd.
hhhhan
haaaaannnn
h1f:gfdgfg
hghjhk:1f:hjjkuhhj
# Ctrl-Alt-Delete is handled by /etc/systemd/system/ctrl-alt-del.target
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
# graphical.target: analogous to runlevel 5
[root@localhost grep]# 

特殊符号的总结

  • 小数点 . 表示任意一个任意字符
  • 星号 * 表示0个或多个星号*前面的字符
  • .* 表示通配,所有的都匹配,(不管是否有字符,都会匹配)
  • {} 一个范围,表示{}花括号前面的一个范围
  • +号,表示一个或或多个+号前面的字符
  • ?号,表示?问号前面0个或一个问号前面的字符
  • |竖线,表示或者

grep扩展

  • 扩展
  • 把一个目录下,过滤所有*.php文档中含有eval的行
  • grep -r --include="*.php" 'eval' /data/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • grep用法
    • grep命令 'r.o'
      • grep命令 'r*o'
        • grep命令 '.*'
          • grep命令 'h{2}' 连续匹配两次h
            • egrep命令 'h{2}' 连续匹配两次h
              • grep命令 -E参数 '(hh){2}'
                • grep命令 'h+h'
                  • grep命令 'h?1f'
                    • grep命令 'root|nologin'
                    • 特殊符号的总结
                    • grep扩展
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档