bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

怎么在shell中使用正則表達式-創新互聯

怎么在shell中使用正則表達式?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

創新互聯公司長期為上千家客戶提供的網站建設服務,團隊從業經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯網生態環境。為漣源企業提供專業的成都網站設計、做網站,漣源網站改版等技術服務。擁有十多年豐富建站經驗和眾多成功案例,為您定制開發。

正則表達式概述


正則表達式是一種定義的規則,Linux工具可以用它來過濾文本。

基礎正則表達式


純文本


[root@node1 ~]# echo "this is a cat" | sed -n '/cat/p'
this is a cat
[root@node1 ~]# echo "this is a cat" | gawk '/cat/{print $0}'
this is a cat

 正則表達式的匹配非常挑剔,尤其需要記住,正則表達式區分大小寫。

特殊字符


正則表達式識別的特殊字符包括:

.*[]^${}\+?|()

如果要使用某個特殊字符作為文本字符,就必須轉義,一般用(\)來轉義。

[root@node1 ~]# echo "this is a $" | sed -n '/\$/p'
this is a $

錨字符


有兩個特殊字符可以用來將模式鎖定在數據流的行首或行尾

脫字符(^)定義從數據流中文本行的行首開始的模式。

美元符($)定義了行尾錨點。

[root@node1 ~]# echo "this is a cat" | sed -n '/^this/p'
this is a cat
[root@node1 ~]# echo "this is a cat" | sed -n '/cat$/p'
this is a cat

在一些情況下可以組合使用這兩個命令

1.比如查找只含有特定文本的行

[root@node1 ljy]# more test.txt  
this is a dog
what
how
this is a cat
is a dog
[root@node1 ljy]# sed -n '/^is a dog$/p' test.txt
is a dog
[root@node

2.兩個錨點組合起來,可以直接過濾空白行

[root@node1 ljy]# more test.txt  
this is a dog
what
how
 
this is a cat
is a dog
[root@node1 ljy]# sed '/^$/d' test.txt 
this is a dog
what
how
this is a cat
is a dog

點號字符


點號用來匹配除換行符外的任意單個字符,他必須匹配一個字符。

[root@node1 ljy]# more test.txt
this is a dog
what
how
this is a cat
is a dog
at
[root@node1 ljy]# sed -n '/.at/p' test.txt
what
this is a cat

字符組


限定待匹配的具體字符,使用字符組。使用方括號來定義一個字符組。

[root@node1 ljy]# more test.txt
this is a dog
this is a Dog
this is a DoG
this is a cat
[root@node1 ljy]# sed -n '/[dD]og/p' test.txt
this is a dog
this is a Dog
[root@node1 ljy]# sed -n '/[dD]o[gG]/p' test.txt 
this is a dog
this is a Dog
this is a DoG

排除型字符組


要排除某些特定的元素,要在字符組前面加個脫字符。

[root@node1 ljy]# sed -n '/[dD]o[gG]/p' test.txt 
this is a dog
this is a Dog
this is a DoG
[root@node1 ljy]# sed -n '/[^D]og/p' test.txt 
this is a dog

區間


正則表達式會包括此區間內的任意字符。

[root@node1 ljy]# more test.txt
123123
1231
121222222
412345341613
vsdvs
qwer12344123
12345
34211
444444
[root@node1 ljy]# sed -n '/^[0-9][0-9][0-9][0-9][0-9]$/p' test.txt
12345
34211

拓展正則表達式


問號


問號表明前面的字符出現0次或者1次,僅限于此。

[root@node1 ljy]# echo "bat" | gawk '/ba?t/{print $0}' 
bat
[root@node1 ljy]# echo "baat" | gawk '/ba?t/{print $0}'
[root@node1 ljy]# echo "bt" | gawk '/ba?t/{print $0}' 
bt

可以將問號和字符組一起使用

[root@node1 ljy]# echo "bt" | gawk '/b[ae]?t/{print $0}'
bt
[root@node1 ljy]# echo "bat" | gawk '/b[ae]?t/{print $0}'
bat
[root@node1 ljy]# echo "bet" | gawk '/b[ae]?t/{print $0}'
bet
[root@node1 ljy]# echo "baat" | gawk '/b[ae]?t/{print $0}'

加號


加號表明前面的字符可以出現一次或多次,但至少是1次。

[root@node1 ljy]# echo "baat" | gawk '/b[ae]+t/{print $0}'
baat
[root@node1 ljy]# echo "bt" | gawk '/b[ae]+t/{print $0}' 
[root@node1 ljy]# echo "bt" | gawk '/ba+t/{print $0}' 
[root@node1 ljy]# echo "bat" | gawk '/ba+t/{print $0}'
bat
[root@node1 ljy]# echo "baat" | gawk '/ba+t/{print $0}'
baat

花括號


ERE中的花括號允許你為可重復的正則表達式規定上下限。

m,n最少出現m此,最多出現n次。

[root@node1 ljy]# echo "baat" | gawk '/b[ae]{1,2}t/{print $0}' 
baat
[root@node1 ljy]# echo "baaat" | gawk '/b[ae]{1,2}t/{print $0}'

管道符號


用邏輯or的方式指定正則表達式規則,其中一個條件符合要就即可。

表達式分組


正則表達式分組也可以用圓括號進行分組。

[root@node1 ljy]# echo "bat" | gawk '/b(a|e)t/{print $0}'  
bat
[root@node1 ljy]# echo "baat" | gawk '/b(a|e)t/{print $0}'
[root@node1 ljy]# echo "bet" | gawk '/b(a|e)t/{print $0}' 
bet

關于怎么在shell中使用正則表達式問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創新互聯行業資訊頻道了解更多相關知識。

分享名稱:怎么在shell中使用正則表達式-創新互聯
URL網址:http://vcdvsql.cn/article22/ceohcc.html

成都網站建設公司_創新互聯,為您提供做網站移動網站建設自適應網站關鍵詞優化外貿建站網站設計公司

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站優化排名