grep:Global search REgular expression and Print out the line
文本搜索工具,根據用戶指定的”模式“對目標文本逐行進行匹配檢查;打印匹配到的行
模式:由正則表達式字符及文本字符所編寫的過濾條件
grep命令選項:
–color=auto:對匹配到的文本著色顯示;
-v:顯示不能夠被匹配到的行;
-i:忽略字符的大小寫
-n:顯示匹配到的行號
-c:統計匹配的行數
-o:僅顯示匹配到的字符串
-q:靜默模式,不輸出任何信息
-A#:after,后#行
-B:before,前#行
-C:context,前后各#行
-e:實現多個選項間的邏輯or關系
grep -e ’cat‘ -e ‘dog’file
-w:整行匹配整個單詞
-E:使用ERE
元字符分類:字符匹配,匹配次數,位置錨定,分組
字符匹配:
. :匹配任意單個字符
[] :匹配指定范圍內的任意單個字符
[^]:匹配指定范圍內的任意單個字符
[:diget:]數字 [:lower:]小寫字母 [:upper:]大寫字母 [:alpha:] 任意字母 [:alnum:] 數字和字母 [:space:]空格 [:punct:]標點符號
匹配次數:
*:匹配前面的字符任意次 eg:a*b 匹配a任意次
.*:任意長度的任意字符
\?:匹配其前面的字符0次或1次
\+:匹配其前面的字符至少1次
\{m\}:匹配其前面的字符m次
\{m,n\}:匹配其前面的字符至少M次,至多n次
\{,n\}:匹配其前面的字符至多n次
\{m,\}:匹配其前面的字符至少M次
位置錨定:
^:行首錨定,用于模式的最左側
$:行尾錨定,用于模式的最右側
^$:空行
^[[:space:]]$空白行
\<或\b:詞首錨定,用于單詞的左側
\>或\b:詞尾錨定,用于單詞的右側
\<PATTERN\>:匹配整個單詞 eg: \<root\>\ 或 \broot\b
分組:\(\):將一個或多個字符捆綁在一起,當作一個整體進行處理,如:\(root\)\+
分組括號中的模式匹配到的內容會被正則表達式引擎記錄于內部的變量中,這些變量的命名方式為: \1, \2, \3, …
\1: 從左側起,第一個左括號以及與之匹配右括號之間的模式所匹配到的字符;
實例:\(string1\+\(string2\)*\)
\1: string1\+\(string2\)*
\2: string2
后向引用:引用前面的分組括號中的模式所匹配字符(而非模式本身)
egrep 擴展的正則表達式
擴展正則表達式的元字符:
字符匹配:
.任意單個字符
[]指定范圍的字符
[^]不在指定范圍的字符
次數匹配
*:匹配前面的字符任意次
?:0或1次
+:1次以上
{m}:匹配m次
{m,n}:至少m次至多n次
位置錨定:
^:行首錨定
$:行尾錨定
\<,\b:詞首錨定
\>,\b:詞尾錨定
分組:() 后向引用:\1,\2……
或a|b C|cat:c或cat
(C|c)at:Cat或cat
[root@localhost ~]# tail -n 0 -f /var/log/messages & 只顯示新的監控內容 (后臺執行)不影響新的操作
剪切df下%前的數字
1[root@localhost ~]# df |tr -s " " ":"|cut -d: -f5|tr -d "%"
Use
2
0
19
21
1
2、查出分區空間使用率的最大百分比值
[root@localhost ~]# df |tr -s ' ' ':'|cut -d: -f5|sort -n -r
3查出/tmp的權限,以數字方式顯示
[root@localhost ~]# stat /tmp|head -4|tail -1|cut -d: -f2|tr -dc '[[:digit:]]'
4查出用戶UID最大值的用戶名、UID及shell類型
[root@localhost ~]# cat /etc/passwd |cut -d: -f 1,3,7|sort -t: -k2 -nr
練習:grep
?1、顯示/proc/meminfo文件中以大小s開頭的行;(要求:使用兩種方式)
[root@localhost ~]# egrep '^(s|S)' /proc/meminfo
[root@localhost ~]# grep ^[sS] /proc
[root@localhost ~]# grep -i "^s" /proc/meminfo
?2、顯示/etc/passwd文件中不以/bin/bash結尾的行
[root@localhost ~]# grep -v /bin/bash$ /etc/passwd
?3、顯示用戶rpc默認的shell程序
[root@localhost ~]# grep '^rpc\b' /etc/passwd |cut -d: -f7
?4、找出/etc/passwd中的兩位或三位數
[root@localhost ~]# grep "\b[0-9]\{2,3\}\b" /etc/passwd
[root@localhost ~]# grep "\b[1-9][0-9]\{1,2\}\b" /etc/passwd
?5、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行
[root@localhost ~]# grep "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg
?6、找出"netstat -tan"命令的結果中以'LISTEN'后跟0、1或多個空白字符結尾的行
[root@localhost ~]# netstat -tan |grep "LISTEN[[:space:]]$\?"
?7、添加用戶bash、testbash、basher以及nologin(其shell為/sbin/nologin),而后找出/etc/passwd文件中用戶名同shell名的行
[root@localhost ~]# grep '^\b\(.*\)\b.*/\1$' /etc/passwd
[root@localhost ~]# grep '^\<\(.*\)\>.*\b\1\b$' /etc/passwd
[root@localhost ~]# grep '^\(.*\).*/\1$' /etc/passwd
練習egrep
?1、顯示當前系統root、mage或wang用戶的UID和默認shell
root@localhost ~]# egrep "^\<root\>|^\<mage\>|^\<wang\>" /etc/passwd | cut -d: -f3,7
[root@localhost ~]# egrep "^(root|mage|wang)\b" /etc/passwd |cut -d: -f3,7
?2、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
[root@localhost ~]# grep -E "^[[:alnum:]_]+\(\)" /etc/rc.d/init.d/functions
?3、使用egrep取出/etc/rc.d/init.d/functions中其基名
init.d/function" |egrep -o "[^/]+/?$"
原創文章,作者:蟄伏,如若轉載,請注明出處:http://www.www58058.com/30303