国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Linux Shell > 正文

shell高級學(xué)習(xí)之正則表達式

2020-07-27 18:44:52
字體:
供稿:網(wǎng)友

正則表達式概述

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

基礎(chǔ)正則表達式

純文本

[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

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

特殊字符

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

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

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

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

錨字符

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

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

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

[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 dogwhathowthis is a catis a dog[root@node1 ljy]# sed -n '/^is a dog$/p' test.txtis a dog[root@node

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

[root@node1 ljy]# more test.txt  this is a dogwhathow this is a catis a dog[root@node1 ljy]# sed '/^$/d' test.txt this is a dogwhathowthis is a catis a dog

點號字符

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

[root@node1 ljy]# more test.txtthis is a dogwhathowthis is a catis a dogat[root@node1 ljy]# sed -n '/.at/p' test.txtwhatthis is a cat

字符組

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

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

排除型字符組

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

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

區(qū)間

正則表達式會包括此區(qū)間內(nèi)的任意字符。

[root@node1 ljy]# more test.txt1231231231121222222412345341613vsdvsqwer123441231234534211444444[root@node1 ljy]# sed -n '/^[0-9][0-9][0-9][0-9][0-9]$/p' test.txt1234534211

拓展正則表達式

問號

問號表明前面的字符出現(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

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

[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}'

加號

加號表明前面的字符可以出現(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

花括號

ERE中的花括號允許你為可重復(fù)的正則表達式規(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}'

管道符號

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

表達式分組

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

[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é)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對武林網(wǎng)的支持。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 潢川县| 沙坪坝区| 利津县| 山西省| 仪陇县| 新巴尔虎左旗| 沙坪坝区| 若尔盖县| 汉源县| 许昌县| 开平市| 阳春市| 合山市| 忻城县| 偏关县| 青龙| 洛阳市| 鹤峰县| 观塘区| 新沂市| 怀集县| 宜宾县| 鹤庆县| 长宁区| 上高县| 南阳市| 望江县| 板桥市| 博兴县| 徐汇区| 葫芦岛市| 永吉县| 湾仔区| 远安县| 辽中县| 华容县| 澜沧| 静海县| 金溪县| 土默特左旗| 洛宁县|