怎么在shell中使用正則表達(dá)式?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)公司長(zhǎng)期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為漣源企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站,漣源網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。正則表達(dá)式概述
正則表達(dá)式是一種定義的規(guī)則,Linux工具可以用它來過濾文本。
基礎(chǔ)正則表達(dá)式
純文本
[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
正則表達(dá)式的匹配非常挑剔,尤其需要記住,正則表達(dá)式區(qū)分大小寫。
特殊字符
正則表達(dá)式識(shí)別的特殊字符包括:
.*[]^${}\+?|()
如果要使用某個(gè)特殊字符作為文本字符,就必須轉(zhuǎn)義,一般用(\)來轉(zhuǎn)義。
[root@node1 ~]# echo "this is a $" | sed -n '/\$/p' this is a $
錨字符
有兩個(gè)特殊字符可以用來將模式鎖定在數(shù)據(jù)流的行首或行尾
脫字符(^)定義從數(shù)據(jù)流中文本行的行首開始的模式。
美元符($)定義了行尾錨點(diǎn)。
[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
在一些情況下可以組合使用這兩個(gè)命令
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.兩個(gè)錨點(diǎn)組合起來,可以直接過濾空白行
[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
點(diǎn)號(hào)字符
點(diǎn)號(hào)用來匹配除換行符外的任意單個(gè)字符,他必須匹配一個(gè)字符。
[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
字符組
限定待匹配的具體字符,使用字符組。使用方括號(hào)來定義一個(gè)字符組。
[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
排除型字符組
要排除某些特定的元素,要在字符組前面加個(gè)脫字符。
[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
區(qū)間
正則表達(dá)式會(huì)包括此區(qū)間內(nèi)的任意字符。
[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
拓展正則表達(dá)式
問號(hào)
問號(hào)表明前面的字符出現(xiàn)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
可以將問號(hào)和字符組一起使用
[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}'
加號(hào)
加號(hào)表明前面的字符可以出現(xiàn)一次或多次,但至少是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
花括號(hào)
ERE中的花括號(hào)允許你為可重復(fù)的正則表達(dá)式規(guī)定上下限。
m,n最少出現(xiàn)m此,最多出現(xiàn)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}'
管道符號(hào)
用邏輯or的方式指定正則表達(dá)式規(guī)則,其中一個(gè)條件符合要就即可。
表達(dá)式分組
正則表達(dá)式分組也可以用圓括號(hào)進(jìn)行分組。
[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
關(guān)于怎么在shell中使用正則表達(dá)式問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
分享名稱:怎么在shell中使用正則表達(dá)式-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://muchs.cn/article22/ceohcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、關(guān)鍵詞優(yōu)化、外貿(mào)建站、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容