Linux學習-正則表達式
一、文本過濾工具grep
格式:grep + [選項] + 匹配內容 + [文件]
--color=auto 關鍵字高亮顯示
-v 顯示不被匹配到的行
-i 忽略大小寫
-n 顯示匹配的行號 cat /etc/passwd |grep -n root
-c 顯示匹配的行數
-o 僅顯示匹配的字符串
-q 靜默模式,不顯示任何信息
可以用在當我們關心的是執行是否成功,不關心執行結果的時候
grep -q shuguo /etc/group && echo exist || echo not exist 存在則輸出exist,不存在則輸出not exist
-A + 數字 顯示匹配行和向下相鄰指定數字的行
cat /etc/passwd |grep -A 3 root
-B + 數字 顯示匹配行和向上相鄰指定數字的行
-C + 數字 顯示匹配行和向上和向下相鄰指定數字的行
-e 多個選項之間or關系
grep -e root -e bin /etc/passwd
-w 僅匹配整個單詞
-E或egrep 支持正則表達式
grep -E "root|bin" /etc/passwd
egrep "root|bin" /etc/passwd
-F或fgrep 不支持正則表達式
二、正則表達式
字符匹配
. 匹配任意單個字符
[] 匹配範圍內的任意單個字符
[^] 匹配範圍外的任意單個字符
[:alnum:] [:alpha:] [:lower:] [:upper:] [:digit:] [:punct:]
[:blank:] 空格和制表符
[:space:] 水平和垂直的空白字符(比[:blank:]範圍廣)
次數匹配 用在要指定次數的字符後面
* 匹配前面的字符任意次,包括0次
例如: .* 任意長度的任意字符
\? 匹配前面的字符0或1次
\+ 匹配前面的字符至少1次
\{n\} 匹配前面的字符n次
\{m,n\} 匹配前面的字符至少m次,至多n次
\{,n\} 匹配前面的字符至多n次
\{m,\} 匹配前面的字符至少m次
位置錨定 定位出現的位置,到哪裏開始,到哪裏結束
^ 行首錨定
$ 行尾錨定
例如:匹配以#開頭,以0結尾,中間內容任意
grep "^#.*0$"
匹配以#開頭,以0結尾
grep "^#0$"
^$ 空行
^[[:space:]]*$ 空白行
\< 詞首錨定
\> 詞尾錨定
分組
\(\) 將一個或多個字符捆綁在一起,當做一個整體進行處理
例如:grep "[ab]\{2\}" 表示a或者b匹配兩次
grep "\(ab\)\{2\}" 表示ab這個整體匹配兩次
分組括號內匹配到的內容會記錄於變量中,這些變量的命名為\1,\2...
\(string1\+\(string2\)\)
\1 表示 string1\+\(string2\)
\2 表示 string2
\| 或
a\|b a或b
\(C\|c\)at Cat或cat
三、案例
1、顯示/proc/meminfo文件中以大小s開頭的行
cat /proc/meminfo |grep "^s\|^S"
cat /proc/meminfo |grep -i ^s
grep ^[sS] /proc/meminfo
grep -e ^s -e ^S /proc/meminfo
2、顯示/etc/passwd文件中不以/bin/bash結尾的行
cat /etc/passwd |grep -v "/bin/bash$"
3、顯示用戶rpc默認的shell程序
grep "^\<rpc\>" /etc/passwd |cut -d: -f7
grep "^rpc\>" /etc/passwd |cut -d: -f7
grep "^\(rpc\)" /etc/passwd |cut -d: -f7
前兩個結果一樣,是題目要求的結果
註意區別
4、找出/etc/passwd中的兩位或三位數
cat /etc/passwd |grep -o "[[:digit:]]\{2,3\}"
cat /etc/passwd |grep -o "[[:digit:]]\{2\}\|[[:digit:]]\{3\}"
5、顯示CentOS7的/etc/grub2.cfg文件中,至少以一個空白字符開頭的且後面存非空白字符的行
cat /etc/grub2.cfg | grep "^[[:space:]]\+[^[:space:]]"
6、找出“netstat -tan”命令的結果中以‘LISTEN’後跟任意多個空白字符結尾的行
netstat -tan | grep "LISTEN[[:space:]]\+$"
7、顯示CentOS7上所有系統用戶的用戶名和UID
cat /etc/passwd |cut -d: -f1,3 | grep "\<[0-9]\{1,3\}\>"
8、添加用戶bash、testbash、basher、sh、nologin(其shell為/sbin/nologin),找出/etc/passwd用戶名同shell名的行
useradd bash
useradd testbash
useradd basher
useradd sh
useradd -s /sbin/nologin nologin
cat /etc/passwd |grep "^\(.*\)\>.*\<\1$"
9、利用df和grep,取出磁盤各分區利用率,並從大到小排序
df |grep "^/dev/sd" |grep -o "[[:digit:]]\{1,3\}%" |tr -d % |sort -nr
四、擴展正則表達式
egrep = grep -E
egrep的字符匹配,次數匹配,位置錨定,分組和grep的區別就是去掉符號前邊的\,除了\<,\>,\b不變
案例:
1、顯示三個用戶root、mage、wang的UID和默認shell
cat /etc/passwd |cut -d: -f1,3,7 |grep -w "root\|mage\|wang"
cat /etc/passwd |cut -d: -f1,3,7 |grep "^\(root\|mage\|wang\)\>"
2、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)後面跟一個小括號的行
cat /etc/rc.d/init.d/functions |egrep "^[_[:alpha:]]+\(\)"
3、使用egrep取出/etc/rc.d/init.d/functions中其基名
echo /etc/rc.d/init.d/functions |egrep -o "\<[[:alpha:]]*\>$"
echo /etc/rc.d/init.d/functions |egrep -o "\<[a-z]*\>$"
4、使用egrep取出/etc/rc.d/init.d/functions/的目錄名
echo /etc/rc.d/init.d/functions/ |egrep -o ".*/." |egrep -o ".*/"
5、統計last命令中以root登錄的每個主機IP地址登錄次數
last |grep "root" |tr -s " " : |cut -d: -f3 |uniq -c |sort -u
6、將此字符串:welcome to magedu linux 中的每個字符去重並排序,重復次數多的排到前面
echo welcome to magedu linux |grep -o . |sort |uniq -c |sort -nr
Linux學習-正則表達式