正則表達式:
由一類特殊字符及文本字符所編寫的模式,其中有些字符(元字符)不表示字符
字面意義,而表示控制或通配的功能
程序支持: grep,sed,awk,vim,less,nginx,varnish 等
元字符分類:字符匹配,匹配次數,位置錨定,分組
man 7 regex 獲取幫助
. 匹配任意單個字符 (也可以ls a? ls a?? ls *匹配任意一個單一字符)# echo adc |grep .
# grep roo. /etc/passwd
[] 匹配指定范圍內的任意單個字符 # echo ac bc fc |grep [ab]c
# echo 1 2 3 4 5 6 3 7 fc |grep [1-5]
# echo 1 4 5 6 7 v 1l a b n |grep [1-6][a-l]
[^] 匹配指定范圍外的任意單個字符
[:alnum:] 或 [0-9a-zA-Z] # echo a d c b 1 2 3 | grep “[[:alnum:]]” # echo a d c b 1 2 3 ? | grep [^[:alnum:]]
[:alpha:] 或 [a-zA-Z]
[:upper:] 或 [A-Z] 大寫字母 # echo a d c b 1 2 3 E ? | grep “[[:digit:][:upper:]]”
[:lower:] 或 [a-z]
[:blank:] 空白字符(空格和制表符)
[:space:] 水平和垂直的空白字符(比[:blank:]包含的范圍廣)
[:cntrl:] 不可打印的控制字符(退格、刪除、警鈴…)
[:digit:] 十進制數字 或[0-9] # echo a d c b 1 2 3 | grep [[:digit:]]
[:xdigit:]十六進制數字
[:graph:] 可打印的非空白字符
[:print:] 可打印字符
[:punct:] 標點符號
匹配次數: 用在要指定次數的字符的字符后面,用于指定前面的字符要出現的次數
先編寫一個文件 cat > regex.txt 編寫內容
匹配前面的字符任意任意次,包含0次 # cat regex.txt|grep g[o]le
貪婪模式:盡可能長的匹配
.任意長度的任意字符 # cat regex.txt|grep g.le
\?匹配其前面的字符0或1次 # cat regex.txt|grep “g[o]\?gle”
+匹配其前面的字符至少1次 # cat regex.txt|grep “g[o]+gle”
{n}匹配其前面的字符n次 # cat regex.txt|grep “g[o]{4}gle”
{m,n}匹配其前面的字符至少m次 至多n次 # cat regex.txt|grep “g[o]{1,10}gle”
{,n}匹配其前面的字符 至多n次 # cat regex.txt|grep “g[o]{,10}gle”
{n,}匹配其前面的字符 至少n次 # cat regex.txt|grep “g[o]{1,}gle”
# cat regex.txt|grep g[o]le gole
# cat regex.txt|grep g[o][o]le goole
^行首錨定,用于模式的最左側 # grep ^root /etc/passwd
$行尾錨定,用于模式的最右側 # grep /bin/bash$ /etc/passwd
^PATTERN$用于模式匹配整行
^$空行 #cat /etc/httpd/conf/httpd.conf |grep -ve “^$” -e “^#”
#cat /etc/httpd/conf/httpd.conf |grep -v “^$|^#”
^[[:space:]]*$空白行
\<或\b 詞首錨定,用于單詞模式的左側
\>或\b 詞尾錨定,用于單詞模式的右側
\<PATTERN\>匹配整個單詞
# grep “\<root\>” /etc/passwd
# grep “^root\>” /etc/passwd
# grep “^root\b” /etc/passwd
# grep “^root:\b” /etc/passwd
分組 :()將一個或多個字符捆綁在一起,當做一個整體進行處理 如: “(root)+”
# grep “(\root)” /etc/passwd
\1表示從左側起第一個左括號以及與之匹配右括號之間的模式所匹配到的字符
如: (string1+string2))
\1:string1+(string2)
\2 :string2
寫一個腳本 test.txt
#cat test.txt |grep “(r..t)”
#cat test.txt |grep “(r..t).\1″ 必須以root開頭并以root結尾
root,dig,raat,root,rooo 前四個顯示 必須以root開頭并以root結尾
root,dig,raat,rooo,root全部顯示 必須以root開頭并以root結尾
或者| a|: a或b C|cat: C或cat
練習:
1、顯示/proc/meminfo文件中以大小s開頭的行(要求:使用兩種方法)
#cat /proc/meminfo|grep “^[Ss]”
#cat /proc/meminfo|grep -i “^s”
#cat /proc/meminfo|grep -e ^s -e ^S
#cat /proc/meminfo|grep “^s|^S”
#cat /proc/meminfo|grep “^[s|S]”
2、顯示/etc/passwd文件中不以/bin/bash結尾的行
#grep -v “/bin/bash$” /etc/passwd
3、顯示用戶rpc默認的shell程序
#grep “^rpc\>” /etc/passwd | cut -d : -f7
#grep -w “^rpc” /etc/passwd | cut -d : -f7
4、找出/etc/passwd中的兩位或三位數
#cat /etc/passwd |grep -o “\<[0-9]{2,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 “\<[[:digit:]]{1,3}\>”$
8、添加用戶bash、testbash、basher、sh、nologin(其shell
為/sbin/nologin),找出/etc/passwd用戶名同shell名的行
#cat /etc/passwd | grep “(^.)\>.\/\1$”
9、僅利用df和grep和sort,取出磁盤各分區利用率,并從大到小排序
#df |grep ^/dev/sd |grep -o “\b[[:digit:]]{1,3}\b%”|sort -rn
擴展的正則表達式
egrep 或grep -E 可以省略\
除了\< \>
\b \b
不可以省略
作業:
1、顯示三個用戶root、mage、wang的UID和默認shell
#cat /etc/passwd|grep -E “^(root|wang|mage)\>”|cut -d : -f3,7
#cat /etc/passwd|grep -E -w “^(root|wang|mage)”|cut -d : -f3,7
2、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
# cat /etc/rc.d/init.d/functions | egrep “^[[:alpha:]_]+\>()”
# cat /etc/rc.d/init.d/functions | grep -o “^.*[:graph:]“
# cat /etc/rc.d/init.d/functions | grep -o “^.*\>\(\)”
3、使用egrep取出/etc/rc.d/init.d/functions中其基名
# echo /etc/rc.d/init.d/functions |egrep “[^/]+/?$”
4、使用egrep取出上面路徑的目錄名
# echo “/etc/rc.d/init.d/functions” |egrep -o ‘.*/\b’
# echo “/etc/rc.d/init.d/functions” |egrep -o ‘.*/\<‘
5、統計last命令中以root登錄的每個主機IP地址登錄次數
# last |grep “^root\>”|egrep -o “([0-9]{1,3}\.){3}[0-9]{1,3}” |sort|uniq -c
6、利用擴展正則表達式分別表示0-9、10-99、100-199、200-249、250-255
#echo {0..300}|egrep -o “\<25[0-5]\>”
[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]
7、顯示ifconfig命令結果中所有IPv4地址
ifconfig | egrep -o “\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4]0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>”
8、將此字符串:welcome to magedu linux 中的每個字符去重并排序,重復次數多的排到前面
# echo “welcome to magedu linux”|grep -o . |sort|uniq -c |sort -nr
原創文章,作者:MOMO,如若轉載,請注明出處:http://www.www58058.com/83520