2018年06月21日 22:42:30 Nick_9705 閱讀數:6731git
我的以爲egrep比較好用,感受改良了grep的一些不能夠直接操做的東西,可是整體來講仍是沒太大區別的,都是一個過濾工具。正則表達式
grep 和 egrep 都要經過 正則表達式來篩選咱們想要的東西,只能篩選文本內容,不能對目錄篩選,若是想篩選目錄能夠經過管道把目錄傳過去,或者把目錄存到文件裏頭再篩選centos
–color=auto:對匹配到的文本着色後高亮顯示bash
這個是grep的別名tcp
alias egrep=’egrep –color=auto’
alias fgrep=’fgrep –color=auto’
alias grep=’grep –color=auto’工具
因此使用的時候默認帶高亮參數spa
若是不想高亮能夠 –color=none.net
-o 是隻顯示匹配到的字符get
[root@localhost tmp]# grep -o 「root」 /tmp/passwd
root
root
root
rootit
-i是忽略大小寫
[root@localhost tmp]# grep -io 「h」 /tmp/h1
H
H
h
h
H
h
-v 是忽略匹配到的行
[root@localhost tmp]# grep -v 「h」 /tmp/h1
HH
若是寫-o和 -v 一塊兒
就不會顯示東西了
我以爲由於是先顯示匹配到的東西,而後再把裏面的東西不匹配的顯示,由於沒有不匹配的,使用就不會顯示東西了
-E 容許使用擴展正則表達式
就是egrep
-q 是靜默模式
就是說明都不輸出
[root@localhost tmp]# grep -q 「root」 /tmp/passwd
[root@localhost tmp]#
-A #:after 後#行
[root@localhost tmp]# grep -A2 「root」 /tmp/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
—
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
後2行一塊兒輸出
-B #:before 前#行
[root@localhost tmp]# grep -B2 「root」 /tmp/passwd
root:x:0:0:root:/root:/bin/bash
—
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
沒有的話,就把–
-C #:先後各#行
[root@localhost tmp]# grep -C2 「root」 /tmp/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
—
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
和-A # -B# 同樣
[root@localhost tmp]# grep -A2 -B2 「root」 /tmp/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
—
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
基本正則表達式元字符
分爲四種
一、字符匹配
.:匹配任意單個字符 相似於通配符中的?
[]:匹配指定範圍內的任意單個字符
[^]:匹配指定範圍外的任意單個字符
[:digit:],[:lower:],[:upper:],[:space:]
[:alpha:],[:alnum:],[:punct:]
二、匹配次數:用於要指定匹配其出現次數的字符的後面,用於限制其前面的字符出現的次數,默認工做於貪婪模式:
*:匹配其前面的字符任意次, 0,1,屢次
.*:匹配任意長度的任意字符
\?:匹配其前面的字符0次或者1次,
\+:匹配其前面的字符1次或者屢次,前面的字符至少出現1次
\{m\}:匹配其前面的字符m次
\{m,n\}:匹配前面的字符至少m次至多n次
\{0,n\}:至多匹配n次
\{m,\}:至少匹配m次
三、位置錨定
^:行首錨定:用於模式的最左側
$:行尾錨定:用於模式的最右側
^patten$:用patten來匹配整行
^$:空白行
^[[:space:]]*$:
\<或者\b:詞首錨定,用於單詞模式的左側
\>或者\b:詞尾錨定,用於單子的右側
\<patten\>:匹配完整的單詞
四、分組及引用
:將一個或者多個字符捆綁在一塊兒,看成一個總體進行處理
分組括號中的模式匹配到的內容會被正則表達式引擎記錄於內部的變量中,
\1:模式從左側起,第一個左括號以及與之匹配的右括號之間的模式匹配到的字符
\2:模式從左側起,第二個左括號以及與之匹配的右括號之間的模式匹配到的字符
上面是四個規則,下面有我本身的從網上找的一些練習題目以及結果;
主要的是結合選項以及咱們所寫的模式來實現功能
好比說
一、顯示/proc/meminfo文件中以大小s開頭的行
[root@localhost ~]# grep -i 「^s」 /proc/meminfo
二、顯示/etc/passwd文件中不以/bin/bash結尾的行
[root@localhost ~]# grep -v 「/bin/bash$」 /etc/passwd
三、找出/etc/passwd中的兩位或三位數
[root@localhost ~]# grep 「\b[0-9]\{2,3\}\b」 /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
四、顯示CentOS7的/etc/grub2.cfg文件中,至少以一個空白 字符開頭的且後面存非空白字符的行
[root@localhost ~]# grep 「^[[:space:]]\+[^[:space:]]」 /etc/grub2.cfg
load_env
五、找出「netstat -tan」命令的結果中以‘LISTEN’後跟任意多 個空白字符結尾的行
[root@localhost ~]# netstat -tan | grep 「LISTEN[[:space:]]\+」
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
六、以root開頭和結尾的行
[root@localhost ~]# cat /tmp/test
rootadfafroot
raatadfasadfraat
raotasdfaroat
rootadfafraat
而後咱們使用分組那個規則
[root@localhost ~]# grep 「r..tr..t.*\1」 /tmp/test
rootadfafroot
raatadfasadfraat
利用引用來查看etc下以se結尾的文件
[root@localhost ~]# cat $(find /etc/ -type f | grep 「se$」)
CentOS Linux release 7.4.1708 (Core)
NAME=」CentOS Linux」
VERSION=」7 (Core)」
ID=」centos」
ID_LIKE=」rhel fedora」
VERSION_ID=」7″
PRETTY_NAME=」CentOS Linux 7 (Core)」
ANSI_COLOR=」0;31″
CPE_NAME=」cpe:/o:centos:centos:7″
HOME_URL=」https://www.centos.org/」
BUG_REPORT_URL=」https://bugs.centos.org/」
CENTOS_MANTISBT_PROJECT=」CentOS-7″
CENTOS_MANTISBT_PROJECT_VERSION=」7″
REDHAT_SUPPORT_PRODUCT=」centos」
REDHAT_SUPPORT_PRODUCT_VERSION=」7″
做業:使用echo輸入一個絕對路徑,使用egrep取其路徑名
[root@localhost ~]# echo 「/etc/profile.d/lang.sh」 | grep -o 「^/.*/」
/etc/profile.d/
egrep 和grep -E同樣
egrep 和 grep的功能幾乎同樣,可是使用的是拓展的正則表達式
拓展正則表達式沒有.*了,而後就是少了使用\
好比說
?:0次或者1次 在grep裏頭要寫\?
+:其前面的字符至少出現1次 \+
{m}:其前的字符出現m次 \{m\}
而後加入了或的一個邏輯
a|b:a或者b C|cat:C或者cat (C|c)at:cat或者Cat