9.3 grep(下)

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)php

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命令,其中的符號則須要脫義。bash

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/
相關文章
相關標籤/搜索