shell高級(jí)學(xué)習(xí)之正則表達(dá)式
正則表達(dá)式概述
正則表達(dá)式是一種定義的規(guī)則,Linux工具可以用它來(lái)過(guò)濾文本。
基礎(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ū)分大小寫(xiě)。
特殊字符
正則表達(dá)式識(shí)別的特殊字符包括:
.*[]^${}\+?|()
如果要使用某個(gè)特殊字符作為文本字符,就必須轉(zhuǎn)義,一般用(\)來(lái)轉(zhuǎn)義。
[root@node1 ~]# echo "this is a $" | sed -n '/\$/p' this is a $
錨字符
有兩個(gè)特殊字符可以用來(lái)將模式鎖定在數(shù)據(jù)流的行首或行尾
脫字符(^)定義從數(shù)據(jù)流中文本行的行首開(kāi)始的模式。
美元符($)定義了行尾錨點(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)組合起來(lái),可以直接過(guò)濾空白行
[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)用來(lái)匹配除換行符外的任意單個(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)來(lái)定義一個(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á)式
問(wèn)號(hào)
問(wèn)號(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
可以將問(wèn)號(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
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)我們的支持。
欄 目:正則表達(dá)式
下一篇:Python面向?qū)ο罂偨Y(jié)及類(lèi)與正則表達(dá)式詳解
本文標(biāo)題:shell高級(jí)學(xué)習(xí)之正則表達(dá)式
本文地址:http://www.jygsgssxh.com/a1/zhengzebiaodashi/11168.html


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-11正則表達(dá)式實(shí)現(xiàn)添加、刪除、替換三
- 01-11正則表達(dá)式之匹配數(shù)字范圍
- 01-11python 用正則表達(dá)式篩選文本信息的實(shí)
- 01-11正則表達(dá)式匹配路由的實(shí)現(xiàn)代碼
- 01-11python爬蟲(chóng)正則表達(dá)式之處理?yè)Q行符
- 01-11js正則表達(dá)式 匹配兩個(gè)特定字符間的
- 01-11正則表達(dá)式截取身份證號(hào)碼加密的方
- 01-11python正則表達(dá)式之對(duì)號(hào)入座篇
- 01-11MySQL使用正則表達(dá)式進(jìn)行查詢(xún)操作經(jīng)典
- 01-11詳解正則表達(dá)式實(shí)現(xiàn)二代身份證號(hào)碼
隨機(jī)閱讀
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery


