首頁  >  文章  >  web前端  >  在Linux內正規使用技巧

在Linux內正規使用技巧

php中世界最好的语言
php中世界最好的语言原創
2018-06-09 14:34:551130瀏覽

這次帶給大家在Linux內正規使用技巧,在Linux內正規使用的注意事項有哪些,以下就是實戰案例,一起來看一下。

1、組成

普通字元:普通字串,沒有特殊意義
特殊字元:在正規表示式中具有特殊的意義
正規表示式中常見的meta字元【特殊字元】

2、POSIX BRE【基本】與ERE【擴充】中都有的meta字元

#\ :通常用於開啟或關閉後續字符的特殊意義,如(...)【\是轉義字符,去掉符號的特殊意義,()、{}等在shell中都有特殊的意義】
.和以及.的區別:

[root@localhost ~]# cat -n test.txt
     1  gd
     2  god
    
     4  good
     5  goood
     6  goad
     7
     8 #     7
     8  gboad#.21 gboad#.為空)

[root@localhost ~]# grep -n "." test.txt      

1:gd
2:god

4:good
#5 :goood
6:goad
8:gboad
[root@localhost ~]# grep -n "go.d" test.txt
4:good
6:goad



2.2、 :符合其前字元任意次,如o,可以是沒有o或一個o,也可以是多個o

[root@localhost ~ ]# grep -n "*" test.txt

[root@localhost ~]# grep -n "o*" test.txt
1:gd

2:god
3:
4:good
5:goood
6:goad
7:
8:gboad
[root@localhost ~]# echo "gbad" >>test.txt
[root@localhost ~]# echo "pbad" >>test.txt
[root@localhost ~]# echo "kgbad" >>test.txt
[root@localhost ~]# echo "poad" >>test.txt  
[root@localhost ~]# grep -n "go*" test.txt 【o可以沒有,o前面的g一定要匹配】
1:gd
2:god
4:good
5:goood
6:goad
8:gboad
9:gbad
11:kgbad



##*2.3、. :匹配任意字元(符合所有),可以為空**

#[root@localhost ~]# grep -n ".*" test.txt

1:gd
2:god

3:
4:good
5:goood
6:goad
7:
8:gboad
9:gbad
10:pbad
11:kgbad
12:poad
[root@localhost ~]# grep -n "go.*" test.txt
2:god
4: good
5:goood
6:goad
[root@localhost ~]# grep -n "po.*" test.txt 
12:poad
[root@localhost ~]# echo "pgoad" >>test.txt   
[root@localhost ~]# grep -n "go.*" test.txt  【匹配go後存在任意字符,可為空】
2:god
4:good
5:goood
6:goad
13:pgoad
#[root@localhost ~]
#[root@localhost ~]# grep -n "o. *" test.txt 
2:god
4:good
5:goood
6:goad
8:gboad
12:poad



#2.4、^ :匹配緊接著後面的正規表示式,以...為開頭

[root@localhost tmp]# grep "^root" /etc/passwd

root:x:0:0:root:/root:/bin/bash
[root@localhost tmp]




#2.5、$ :匹配緊接著前面的正規表達式,以...結尾


[root@localhost tmp]# grep "bash$" /etc/passwd | head -1

root:x:0:0: root:/root:/bin/bash
[root@localhost tmp]

#^$:表示是空白行的意思
「#|^$」:符合以#號開頭的註解行和空白行



2.6、[] :在匹配方括號裡的任一字元

(如[sS],匹配s或匹配S),其中可用連字符(-)指定連字符的範圍(如[(0-9)],匹配0-9任一字元);[^0-9]如果^符號出現在方括號的第一個位置,則表示匹配不在列表中的任一字元。


[root@localhost tmp]# cat hosts

192.168.200.1
192.168.200.3

a.b.123.5
23.c.56.1
14561. .2.4
12.4.5.6.8
[root@localhost tmp]# grep -E '([0-9]{1,3}\.){3}[0-9]{1,3 }' hosts  
192.168.200.1
192.168.200.3
1456.1.2.4
12.4.5.6.8
[root@localhost tmp]# grep -#12.4.5.6.8
[root@localhost tmp]# grep -#12.4.5.6.8
[root@localhost tmp]# grep -#12.4.5.6.8
[root@localhost tmp]# grep -#12.4.0-9] {1,3}\.){3}[0-9]{1,3}$' hosts
192.168.200.1
192.168.200.3
[root@localhost tmp]

2.7、? :符合前面字元的零次或多次


#[root@localhost ~]# grep -E "go?d" test.txt  
gd
god
[root@localhost ~]
#[root@localhost tmp]# cat test
do
does
doxy
[root@localhost tmp]# grep -E "do(es)?" test
do
does
doxy
[root@localhost tmp]


#3、 POSIX BRE(基本正規)中才有的字元

####

{n,m} :區間表達式,符合在它前面的單一字元重現【重複,緊接著的單一字元如https{0,1},即重複s 0-1次。 {n}指配對n次;{n,m}指配對n至m次,{n,}指配對至少n次,{,m}符合至多m次。 【\轉義字元】

4、POSIX ERE(擴充正規)中才有的字元

4.1、{n,m} :與BRE的{n,m}功能相同

[root@localhost tmp]# grep -E '^([0-9]{1,3}\.){3}[ 0-9]{1,3}$' hosts
192.168.200.1
192.168.200.3

4.2、 :符合前面正規表示式的一次或多次

[root@localhost ~]# egrep "go d" test.txt
god
good
goood
[root@localhost ~]

#4.3、| :表示符合多個字串【或的關係】

[root@localhost ~]# grep -E "3306|1521 " /etc/services
mysql           3306/tcp                                         # MySQL
ncube-lm              1521/udp                # nCube License Manager
[root@localhost ~]


#4.4、( ) :分組過濾,後向引用

分組過濾   


#[root@localhost ~]# echo "glad" >> test.txt
[root@localhost ~]# egrep "(la|oo)" test.txt

good
goood
glad


()後向引用;當前面匹配部分用小括號的時候,第一個括號的內容可以在後面部分用\1輸出;以此類推。


 [root@localhost tmp]# ifconfig |sed -rn 's#.*addr:(.*)(B.*)$#\1#gp'
192.168.4.27 



#5、正規表示式的元字元


##5.1、\b :符合一個單字邊界

[root@localhost tmp]# cat test       

do
does

doxy
agdoeg
[root@localhost tmp]# grep "do\b" test
do
[root@localhost tmp]# grep "\bdo" test       
do
does
doxy
[root@localhost tmp]# grep #does
[root@localhost tmp]# grep "\bdo\b" test 
do
[root@localhost tmp]



#5.2、 \B :匹配非單字邊界,與\b相反

[root@localhost tmp]# grep "do\B" test   

does

doxy
agdoeg

[root@localhost tmp]# grep "do\b" test
do
[root@localhost tmp]



#5.3、\d :符合一個數字字符,等價於[0-9]

5.4、\D :匹配一個非數字字符,等價於[^0-9]

5.5、\w :匹配字母、數字、底線,等價於[A-Za-z0-9_]

還有很多元字符,這裡就不一一羅列出來

案例:開機精簡

#[root@localhost ~]# chkconfig --list| egrep -v "crond|network|rsyslog|sshd|sysstat" | awk '{print "chkconfig",$1,"off"}'|bash

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!


推薦閱讀

如何操作Angular專案內使用scss

#axios中的302狀態碼


##

以上是在Linux內正規使用技巧的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn